1

我在 J2ME 中创建了一个应用程序,它使用以下方法连接到服务器。php 在服务器端用于处理请求。该应用程序在诺基亚 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;
}
4

0 回答 0