BlackBerry手机向Web服务器提交数据,可以通过HttpConnection的方式发送,写了一个方法,直接调用即可,代码如下:
public void postAgent() throws IOException{
InputStream istrm = null;
HttpConnection hconn = null;
OutputStream out = null;
try
{
String bodystr = "发送的内容";
String URL = http://127.0.0.1/mycode;
hconn = (HttpConnection)Connector.open(URL);
hconn.setRequestMethod(HttpConnection.POST);
hconn.setRequestProperty("User-Agent", "BlackBerry/4.2.0");
hconn.setRequestProperty("Content-Length",String.valueOf(bodystr.getBytes().length));
hconn.setRequestProperty("Content-Type","text/xml;charset=gb2312");
out = hconn.openOutputStream();
out.write(bodystr.getBytes());
if(hconn.getResponseCode()==HttpConnection.HTTP_OK)
{
istrm = hconn.openInputStream();
int length = (int) hconn.getLength();
if(length>0)
{
byte servletData[] = new byte[length];
istrm.read(servletData);
}
else
Dialog.alert("错误");
}
Dialog.alert("发送完毕");
}
catch(Exception e)
{System.out.println(e.toString());}
finally
{
if(istrm!=null)
istrm.close();
if(out!=null)
out.close();
if(hconn!=null)
hconn.close();
}
}