1

我无法使用 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");
    }

}
4

3 回答 3

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;
    }
于 2012-08-15T16:13:01.513 回答
0

根据这个诺基亚示例,您不需要调用setRequestProperty方法。

另一个有趣的地方是.Connector.open(urlstring, Connector.READ_WRITE)

于 2012-08-14T20:03:34.540 回答
0

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

于 2013-11-26T11:54:40.260 回答