java - J2ME 应用程序不发送 POST 请求

标签 java java-me emulation

我现在有点迷茫。

我有一个将 POST 请求发送到 servlet 的 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;
    }
}

谢谢。

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

最佳答案

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

违规行是

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

代替

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

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

那次改变之后,世界又变美了

关于java - J2ME 应用程序不发送 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7455891/

相关文章:

java - 使用 javax.print 通过 cups 打印

netbeans - 如何将 LWUIT 与 NetBeans IDE 7.1.2 一起使用?

java-me - 如何卸载诺基亚连接框架 (NCF?) - 无法安装诺基亚 SDK 1.0 for Java

android - 如何从代码中找出我的 Android 应用程序是在模拟器还是真实设备上运行?

java - 使用 Scanner 时出现 NoSuchElementException

方法变量和包名称之间的 Java 命名冲突

Xamarin AVD 存储问题

c++ - 使用 C++03 模拟 Variadic 模板时,我们可以在类中使用不同的代码吗?

java - 如何表示GAME浏览器游戏的棋盘/ map 数据?

java - 如何使用 J2ME (CDC/PP 1.1) 创建 alphablending BufferedImage