【问题标题】:J2ME App not sending POST requestsJ2ME 应用程序不发送 POST 请求
【发布时间】:2011-09-17 15:37:16
【问题描述】:

我现在有点迷茫。

我有一个向 servlet 发送 POST 请求的 J2ME 应用程序。它一直工作到今天早上奇迹般地停止工作。它只是不会发送 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;
    }
}

谢谢。

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

【问题讨论】:

    标签: java java-me emulation


    【解决方案1】:

    所以大小写很重要。这个帖子Http post from J2ME救了我的命。

    违规行是

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

    代替

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

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

    改变之后,世界又变得美丽了

    【讨论】:

    • 我尝试了该代码,但它给了我“所需长度:411”错误@Aardvocate Akintayo Olusegun
    • 您在哪个手机上测试过这个?我正在使用诺基亚 2700 经典版,http 使用 get、post 都可以正常工作。但是在诺基亚 c6 上尝试时显示需要 411 长度。什么能引起共鸣?
    【解决方案2】:

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

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

    【讨论】:

    • 那些奇怪的字符是我在使用模拟器自带的netmon程序时看到的日志的一部分。 ServerError: null 我在服务器上生成 NullPointerException 时发送给自己,这是因为 post 参数之一为 null。实际上所有的 post 参数都是空的。根据使用 Web 表单进行的测试,我有一个 iPhone 应用程序,它使用相同的参数调用相同的 URL,并且工作正常。我只是想将 iPhone 应用程序转换为 J2ME 应用程序。
    【解决方案3】:

    查看您的代码,我真的看不出它有什么问题。我能够使用这个 sn-p(在下面找到)从 J2ME 应用程序向 Java 后端发出 POST 请求,并且一切正常。我希望这个对你有用。一切顺利。

             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();
            }
    

    【讨论】:

    • 仍然没有快乐的人!!!,我真的开始认为宇宙今天对我不利! :(
    【解决方案4】:

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

    【讨论】:

    • 如果我不发送文件怎么办?我只对发送 POST 请求而不是文件感兴趣。
    猜你喜欢
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 2018-02-09
    • 2019-12-18
    相关资源
    最近更新 更多