1

次の方法を使用してサーバーに接続するアプリケーションを 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;
}
4

0 に答える 0