J2ME App不发送POST请求

时间:2011-09-17 15:37:17

标签: java java-me emulation

我现在有点困惑。

我有一个J2ME应用程序,它将POST请求发送到servlet。它正在努力工作,直到今天早上奇迹停止了工作。它只是不会发送POST数据。它将联系服务器,但请求数据将为空。

System.out.println(request.getParameterMap());

始终返回

{}

我试图从模拟器中检查网络监视器,但是我看到了这个

sq~wLhttp://localhost:80802xѬ2xѬ????????xsq~wLhttp://localhost:80802xѬ2xѭ????????xsq~z?http://localhost:80802xѬK2xѬ????????1316274753996
POST /amw/synch HTTP/1.1
User-Agent: Profile/MIDP-1.0 Confirguration/CLDC-1.0 UNTRUSTED/1.0
Accept_Language: en-US
Content-Type: application/x-www-form-urlencoded
Host: localhost:8080
Transfer-Encoding: chunked

56
&action=recover&email=trinisoftinc@gmail.com&password=1011001&api-key=oalkuisnetgauyno
0

xsq~z?http://localhost:80802xѬD2xѭ????????1316274754009
HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1 Java/Apple Inc./1.6)
Server: GlassFish Server Open Source Edition 3.1
Content-Type: text/html;charset=UTF-8
Content-Length: 46
Date: Sat, 17 Sep 2011 15:52:33 GMT

{"message":"Server Error: null","status":500}

我真的不知道该怎么做。

我需要帮助。

发送请求的代码在

之下
public String post(String url, String query, String optionalParameters) throws IOException {
    if (optionalParameters != null) {
        url += optionalParameters;
    }

    query += "&api-key=" + APIKey;
    Echo.outln(url + ", " + query.getBytes().length);
    Echo.outln(query);
    HttpConnection connection;
    connection = (HttpConnection) Connector.open(url);

    connection.setRequestMethod(HttpConnection.POST);
    connection.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
    connection.setRequestProperty("Accept_Language", "en-US");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("Content-Length", String.valueOf(query.getBytes().length));
    //connection.setRequestProperty("api-key", APIKey);

    OutputStream os = connection.openDataOutputStream();
    os.write(query.getBytes());
    os.flush();
    if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
        InputStream is = connection.openInputStream();
        int ch;
        StringBuffer buffer = new StringBuffer();

        while((ch = is.read()) != -1) {
            buffer.append((char) ch);
        }            

        try {
            is.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toString();
    } else {
        String retval = connection.getResponseMessage() + " : " + connection.getResponseCode();
        try {
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retval;
    }
}

感谢。

注意:我有一个iPhone应用程序,可以调用相同的servlet并且运行良好。

4 个答案:

答案 0 :(得分:2)

所以案件很重要。这篇文章Http post from J2ME救了我的命。

违规行是

connection.setRequestProperty("Content-Length", "" + query.getBytes().length);

而不是

connection.setRequestProperty("Content-length", "" + query.getBytes().length);

注意:小写字母l,不是大写字母L.

在那次改变之后,这个世界又变得美丽了

答案 1 :(得分:0)

首先,我认为我们可以看到POST有一些奇怪的字符(xѬ2xѬ??)。可以很好地看到正确的。

下一点是POST似乎已发送,但服务器的响应是错误的(查看消息“服务器错误:null”,状态为500)。那么,您是否测试过使用简单的HTML表单从浏览器发送相同的帖子?那服务器怎么样?你有没有用错误分析堆栈跟踪?服务器中的请求过程似乎有问题。

答案 2 :(得分:0)

看看你的代码,我真的不知道它有什么问题。我能够使用这个片段(如下所示)从J2ME应用程序发出POST请求到Java后端,一切运行良好。我希望这个对你有用。一切顺利。

         HttpConnection httpConn = null;

        String url = "http://your_resource";        


        InputStream is = null;
        OutputStream os = null;
        String phonenumber ="080380012";
        String data = "Post data";
        try {



                httpConn = (HttpConnection) Connector.open(url);                     
                httpConn.setRequestMethod(HttpConnection.POST);
                httpConn.setRequestProperty("User-Agent",
                                "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
                httpConn.setRequestProperty("Accept_Language", "en-US");                                    

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

                os = httpConn.openOutputStream();       
                String params;
                params = "URL=" + data;
                params += "&Phone=" + phonenumber;

                os.write(params.getBytes());


                os.flush();
    ///parsing the xml response.... this part depends on what you are returning from the server
                    XmlParser parser = new XmlParser(new InputStreamReader(httpConn.openInputStream()));
                org.kxml.kdom.Document doc = new org.kxml.kdom.Document();
                doc.parse(parser);
                   /// work with the parsed data
                }
                catch (Exception ex)
        {
                System.out.println(ex.getMessage()+ "exception Ex");

        }      

        finally {
                if (is != null)
                        is.close();
                if (os != null)
                        os.close();
                if (httpConn != null)
                        httpConn.close();
        }

答案 3 :(得分:0)

我知道发布到网址的最佳方法是使用this tutorial on HttpMultipart。试试吧!