本文介绍了黑莓HttpPost空指针异常的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过以下code,服务器被击中,但我也想在POST方法发送变量。
但它只是给了我一个空指针异常。它有什么做的的OutputStream
。我已经检查 POSTDATA
...它有一定的地址,这意味着它不为空。
那么,为什么我给空指针异常?
连接工厂connFact =新的ConnectionFactory();
ConnectionDescriptor connDesc;
尝试{
connDesc = connFact.getConnection(http://example.com/login.php,Connector.READ_WRITE,returnContent);
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD); URLEn codedPostData encPostData =新URLEn codedPostData(UTF-8,FALSE);
encPostData.append(计数,地狱呀!);
。字节[] = POSTDATA encPostData.toString()的getBytes(UTF-8); httpConn.setRequestProperty(内容长度,将String.valueOf(postData.length));
Dialog.alert(邮报数据:+ POSTDATA); OutputStream的OS = httpConn.openOutputStream();
os.write(POSTDATA);
os.flush();
httpConn =(HttpConnection的)connDesc.getConnection(); 最终诠释iResponse code = httpConn.getResponse code();
UiApplication.getUiApplication()的invokeLater(Runnable的新(){
公共无效的run(){
//Dialog.alert(\"Response code:+ Integer.toString(iResponse code));
}
});
}赶上(IOException异常五){
通信System.err.println(捕获到IOException:+ e.getMessage());
}
解决方案
正在滥用 ConnectionFactory.getConnection(字符串URL,INT transportType,字符串ConnectionUID)
方法。例如,检查你传递什么样的 transportType
...
下面是API说,关于返回值:
So, it just fails and returns null
. And that's why you get a NPE.
UPDATE:
I would recommend to just use a simpler API ConnectionFactory.getConnection(String url)
:
ConnectionDescriptor cd = connFact.getConnection("http://example.com/login.php");
if (cd == null) {
// throw an error signalling there is no connectivity right now
}
HttpConnection httpConn = (HttpConnection) cd.getConnection();
// the rest of the code working with httpConn ..
这篇关于黑莓HttpPost空指针异常的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!