【问题标题】:Unable to do HTTP POST from J2ME (Nokia S40 6thEdition)无法从 J2ME 执行 HTTP POST(诺基亚 S40 第 6 版)
【发布时间】:2013-11-26 19:54:40
【问题描述】:

我无法使用 J2ME 执行 HTTP POST 下面是我尝试写入 OutputStrem 时抛出异常的代码。

在 SYSO 下面的代码中打印“here5”。请需要一些指导。基本上,我将 http 连接部分放在单独的线程运行方法中,以使其远离 UI 线程。

public void run(){
    HttpConnection http = null;
    OutputStream out = null;
    String msg = "lat=10&long=20&mac=923873";
    try{
    String url = "http://xxxx.php"; 
    byte[] data = null;
    InputStream istrm = null;

    http = (HttpConnection)Connector.open(url);
    }
    catch(Exception e)
    {
        System.out.println("here1");
    }
    try
    {
        http.setRequestMethod(HttpConnection.POST);
    }
    catch(Exception e)
    {
        System.out.println("here2");
    }

    try{
        http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        http.setRequestProperty("User-Agent", "HttpMidlet/0.2");
        http.setRequestProperty("Custom-Property", "MyCustomProperty/1.0; AnotherProperty/debug_0.1");

        http.setRequestProperty("Content-Length", ""+msg.getBytes().length);
    }
    catch(Exception e)
    {
        System.out.println("here3");
    }

    try{

        out = http.openOutputStream();
    }
    catch(Exception e)
    {
        System.out.println("here4");
    }
    try{
    out.write(msg.getBytes());
    out.flush();
    }
    catch(Exception e)
    {
        System.out.println("here5");
    }

}

【问题讨论】:

  • 试试DataOutputStream类。
  • 仍然无法解决此错误“0-HTTP 操作错误”
  • 1)System.out.println("here1") 替换为 System.out.println("here1, [" + e + "]) 2) 对所有剩余的四个 printlns 执行相同操作 3) 重新运行您的测试4) 更新您的问题,打印改进的日志消息,而不是神秘的“here5”

标签: java java-me


【解决方案1】:
/**
     * Send the data to the URL of Server Site using the POST connection.
     * 
     * @return the response of server.
     * @throws Exception
     */
    public byte[] send() throws Exception {
        HttpConnection hc = null;
        InputStream is = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        byte[] res = null;

        try {
            hc = (HttpConnection) Connector.open(url);

            hc.setRequestProperty("Content-Type",
                    "multipart/form-data; boundary=" + getBoundaryString());

            hc.setRequestMethod(HttpConnection.POST);



            OutputStream dout = hc.openOutputStream();

            dout.write(postBytes);
            if (dout!=null) {
                dout.close();
                dout = null;
            }

            int ch;
            is = hc.openInputStream();

            while ((ch = is.read()) != -1) {
                bos.write(ch);
            }
            res = bos.toByteArray();
        } catch (Exception e) {
            // if an error occurred connecting to the server.
            throw new Exception(e.getMessage());

        } finally {
            try {
                if (bos != null)
                    bos.close();

                if (is != null)
                    is.close();

                if (hc != null)
                    hc.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return res;
    }

【讨论】:

  • 嗨,非常感谢您的代码。尝试过,但仍然出现相同的错误“HTTP 操作中的 0-Error”
【解决方案2】:

根据这个Nokia Sample,您不需要调用setRequestProperty 方法。

另一个有趣的地方是Connector.open(urlstring, <b>Connector.READ_WRITE</b>)的用法。

【讨论】:

  • 嗨,非常感谢您的链接。尝试过,但仍然出现相同的错误“HTTP 操作中的 0-Error”
  • 请按照@gnat 在问题 cmets 中所说的做。更改您的调试消息以获取有关异常的更多信息。
【解决方案3】:

我通过重新安装模拟器解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多