使用post会在某些手机上抛出IOExceptions

时间:2012-11-22 07:46:13

标签: java-me midp

我在J2ME中创建了一个应用程序,它使用以下方法连接到服务器。 php在服务器端用于处理请求。该应用程序在某些手机上工作正常,如NOkia asha 303但在其他手机上抛出IOException可能是这段代码的问题。在另一个线程上调用此方法。我也想问你是否在j2me中使用post并设置

 httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

你必须在发送之前对数据进行urlencode,或者由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;
}

0 个答案:

没有答案