Java me 在某些手机上使用post会引发异常

Java me 在某些手机上使用post会引发异常,java-me,midp,Java Me,Midp,我在J2ME中创建了一个应用程序,它使用以下方法连接到服务器。服务器端使用php来处理请求。该应用程序在某些手机(如诺基亚asha 303)上运行良好,但在其他手机上引发IOException此代码可能有什么问题。在不同的线程上调用此方法。我还想问您是否在j2me中使用post并设置 httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 您必须在发送之前对数据进行URL编码,还是由

我在J2ME中创建了一个应用程序,它使用以下方法连接到服务器。服务器端使用php来处理请求。该应用程序在某些手机(如诺基亚asha 303)上运行良好,但在其他手机上引发IOException此代码可能有什么问题。在不同的线程上调用此方法。我还想问您是否在j2me中使用post并设置

 httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
您必须在发送之前对数据进行URL编码,还是由j2me自动处理

public String sendData(String serverUrl, String dataToSend) {

    String strResponse = "0"; //string to hold serverResponse
    StringBuffer sb = new StringBuffer("");
    HttpConnection httpConn = null;
    InputStream inputStream = null;
    OutputStream outStream = null;
    try {
        //convert the dataToSend to bytes 
        String strData = dataToSend; //  get the data to Send and store it in a variable.
        byte[] dataToSendBytes = strData.getBytes();

        //open the Connection to the server.
        httpConn = (HttpConnection) Connector.open(serverUrl, Connector.READ_WRITE, true);

        if (httpConn != null) {
            httpConn.setRequestMethod(HttpConnection.POST); // method used to send the data.
            httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpConn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1");
            //httpConn.setRequestProperty("Content-Language", "en-US"); //language to use.
            //setRequestProperty("Connection", "close") ===> could generate or Solve IOExceptions on different mobile Phones.
            //httpConn.setRequestProperty("Connection", "close");
            //setting the Content-length could have issues with some Servers===>
            //if the dataToSend is Empty String => contentLen = 0 , else get length of the dataBytes.
            String contentLen = ((dataToSend.length() > 0) ? Integer.toString(dataToSendBytes.length) : Integer.toString(0));
            //httpConn.setRequestProperty("Content-length", contentLen); //not working on Emulator ....enable on shipping application.

            //open the output Stream to send data to the Server.
            outStream = httpConn.openOutputStream();

            // send the bytes of data to the Server.===> 
            //outStream.write(dataToSendBytes);

            //send the data bytes to the Server.
            int byteLen = dataToSendBytes.length; //length of byteToSend.
            for (int k = 0; k < byteLen; k++) {
                //send all the databytes to the Server.
                outStream.write(dataToSendBytes[k]);
            }

            //close the outputStream ===immediately after sending the data bytes to the Server===>eliminates the IOExceptions.
            closeOutStream(outStream);

            //get response code on Sending Data===> getting response code automatically flushes the output data.
            ntworkResponseCode = httpConn.getResponseCode();

            if (ntworkResponseCode == HttpConnection.HTTP_OK) {
                //connection to the Server was okay.

                if (httpConn != null) {
                    //read the Response From the Server-----------
                    inputStream = httpConn.openInputStream(); // open the inputStream.

                    //get server Response Content length.
                    int contLen = (int) httpConn.getLength();
                    byte[] serverResponseBytes; //byte array to store the serverResponse.
                    if (contLen != -1) {
                        //store the serverResponse to a byte array.===> using a byte buffer could be faster.
                        serverResponseBytes = new byte[contLen];
                        inputStream.read(serverResponseBytes);

                        //convert the bytes to String and store them to the StringBuffer.
                        sb.append(new String(serverResponseBytes));

                    } else {
                        //serverResponse Length not available===> read character by character could be slower.
                        int read;
                        while ((read = inputStream.read()) != -1) {
                            sb.append((char) read);
                        }

                    }

                    //store the server response in a String.
                    strResponse = sb.toString();

                }
            } else {
                //connection problem occured.
                //throw new IOException("Http Response Code [" + ntworkResponseCode + "]");
            }

        }// the httpConnection Not null.--end.

    } catch (IllegalArgumentException arge) {
        strResponse = CONNECTION_EXCEPTION;
    } catch (ConnectionNotFoundException cone) {
        //a string to show we got an exception
        strResponse = CONNECTION_EXCEPTION;
    } catch (IOException ioe) {
        //a string to show we got an exception
        strResponse = CONNECTION_EXCEPTION;
    } catch (SecurityException se) {
        //user cancelled the Connection Request.
        strResponse = CONNECTION_EXCEPTION;
    } finally {
        //close all the connection streams 
        try {
            if (inputStream != null) {
                //close the inputStream
                inputStream.close();
                inputStream = null;
            }

            if (outStream != null) {
                //close the outStream.
                outStream.close();
                outStream = null;
            }

            if (httpConn != null) {
                //close  the connection object.
                httpConn.close();
                httpConn = null;
            }

        } catch (IOException ie) {
              //show exception occured.
        }

    }
    //return server Response to the Client.
    return strResponse;
}

你跟踪哪条线路引发IOException了吗?@Telmo Pimentel Mota我没有跟踪引发IOException的线路,我的问题是,在某些手机上,它运行正常,没有任何异常,但在其他手机上,异常被引发。我怀疑setRequestProperty方法之一。这是否意味着在j2me平台上邮寄不是标准化的。如果是标准化的,就不会出现错误了解引发异常的行是调查问题的第一步。