J2ME App不发送POST请求 [英] J2ME App not sending POST requests

查看:189
本文介绍了J2ME App不发送POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有点困惑。

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

I have a J2ME app that sends POST requests to a servlet. It was working up until miraculous this morning it stopped working. It just won't send the POST data. It will contact the server, but the request data will be empty.

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

始终返回

{}

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

I tried to check the network monitor from the emulator and I saw this

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}

我真的不知道该怎么做。

I didn't know what to make of it really.

我需要帮助。

发送请求的代码低于

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并且运行良好。

NB: I have an iPhone app that calls the same servlet and works perfectly well.

推荐答案

所以案例事项。这篇文章来自J2ME的Http帖子挽救了我的生命。

So case matters. This post Http post from J2ME saved my life.

违规行是

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

而不是

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

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

NOTE: Small letter l, not capital letter L.

在改变之后,世界再次变得美丽

After that change, the world looks beautiful again

这篇关于J2ME App不发送POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆