黑莓HttpPost空指针异常的Java [英] Blackberry HttpPost null pointer exception 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说,关于返回值:
返回:如果可以建立一个连接的ConnectionDescriptor;
否则,返回null
块引用>所以,它只是将失败并返回
空
。这就是为什么你会得到一个NPE。更新
我会建议只使用一个简单的API
ConnectionFactory.getConnection(字符串URL)
:ConnectionDescriptor CD = connFact.getConnection(http://example.com/login.php);
如果(CD == NULL){
//抛出一个错误的信号没有连接现在
}
的HttpConnection httpConn =(HttpConnection的)cd.getConnection();
//将code与httpConn工作的休息..With the following code, the server is getting hit, but I also want to send variables in POST method.
But it just gives me a null pointer exception. It has something to do with the
OutputStream
. I have checkedpostData
... it has some address, which means it is not null.So why I am given null pointer exception?
ConnectionFactory connFact = new ConnectionFactory(); ConnectionDescriptor connDesc; try { connDesc = connFact.getConnection("http://example.com/login.php", Connector.READ_WRITE, returnContent); httpConn.setRequestMethod(HttpConnection.POST); httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false); encPostData.append("count", "Hell Yeah!"); byte[] postData = encPostData.toString().getBytes("UTF-8"); httpConn.setRequestProperty("Content-length", String.valueOf(postData.length)); Dialog.alert("Post Data: " + postData); OutputStream os = httpConn.openOutputStream(); os.write(postData); os.flush(); httpConn = (HttpConnection) connDesc.getConnection(); final int iResponseCode = httpConn.getResponseCode(); UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { //Dialog.alert("Response code: " + Integer.toString(iResponseCode)); } }); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); }
解决方案You are misusing the
ConnectionFactory.getConnection(String url, int transportType, String ConnectionUID)
method. For instance, check what you pass astransportType
...Here is what API says about return value:
Returns: a ConnectionDescriptor if a connection can be established; null otherwise
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!