【发布时间】: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”