Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法从J2ME执行HTTP POST(诺基亚S40 6功能)_Java_Java Me - Fatal编程技术网

Java 无法从J2ME执行HTTP POST(诺基亚S40 6功能)

Java 无法从J2ME执行HTTP POST(诺基亚S40 6功能),java,java-me,Java,Java Me,下面是我使用的代码,当我尝试写入OutputStrem时,它引发了一个异常 在下面的代码中,SYSO打印“here5”。请给我一些指导。基本上,我将http连接部分放在一个单独的线程运行方法中,以使其远离UI线程 public void run(){ HttpConnection http = null; OutputStream out = null; String msg = "lat=10&long=20&mac=923873"; try{

下面是我使用的代码,当我尝试写入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");
    }

}

因此,您不需要调用
setRequestProperty
方法 另一个有趣的地方是
Connector.open(urlstring,Connector.READ\u-WRITE)
的用法,根据这一点,您不需要调用
setRequestProperty
方法 另一个有趣的问题是使用
Connector.open(urlstring,Connector.READ\u WRITE)
我通过重新安装仿真器解决了这个问题。

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

尝试
DataOutputStream
类。验证此解决方案:仍然无法解决此错误“HTTP操作中的0错误”1) 将
System.out.println(“here1”)
替换为
System.out.println(“here1,[“+e+”])
2)对所有剩余的四个
println
s 3)重新运行测试4)使用改进的日志消息更新您的问题,而不是使用晦涩的“here5”“尝试
DataOutputStream
类。验证此解决方案:仍然无法解决此错误“HTTP操作中的0错误”1)将
System.out.println(“here1”)
替换为
System.out.println(“here1,[“+e+”]))
2)对所有剩余的四个
println
s 3)重新运行测试4)使用改进的日志消息更新您的问题,而不是神秘的“here5”嗨,非常感谢代码。尝试了它,但仍然得到相同的错误“HTTP操作中的0-error”嗨,非常感谢代码。尝试了它,但仍然得到相同的错误HTTP操作中的0-Error“您好,非常感谢您提供的链接。尝试了该链接,但仍然得到相同的错误“HTTP操作中的0-Error”请按照@gnat在问题注释中所说的操作。更改调试消息以获取有关异常的更多信息。您好,非常感谢您提供的链接。尝试了该链接,但仍然得到相同的错误“HTTP操作中的0-Error”“请按照@gnat在问题评论中所说的去做。更改调试消息以获取有关异常的更多信息。
/**
     * 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;
    }