我正在使用httpurlconnection测试android中的长轮询,但应用程序会因错误nullpointerexception而崩溃。
07-21 07:17:42.469: V/Online Connection(27092): Bufferd called 07-21 07:17:42.469: V/Online Connection(27092): data called 07-21 07:17:42.471: V/Online Connection(27092): Get data called 07-21 07:17:42.471: E/Online Connection(27092): Error calledSocket closed 07-21 07:17:42.472: V/Online Connection(27092): Get data called 07-21 07:17:42.473: V/Online Connection(27092): Connect called 07-21 07:17:42.479: V/Online Connection(27092): Connect called 07-21 07:17:42.832: V/Online Connection(27092): Bufferd called 07-21 07:17:42.832: V/Online Connection(27092): data calledok 07-21 07:17:44.147: V/Online Connection(27092): Bufferd called 07-21 07:17:44.147: V/Online Connection(27092): data called 07-21 07:17:44.148: V/Online Connection(27092): Get data called 07-21 07:17:44.149: V/Online Connection(27092): Connect called 07-21 07:17:44.152: E/Online Connection(27092): Error calledSocket closed 07-21 07:17:44.205: E/AndroidRuntime(27092): FATAL EXCEPTION: Thread-34364 07-21 07:17:44.205: E/AndroidRuntime(27092): Process: in.briskjab.reactor, PID: 27092 07-21 07:17:44.205: E/AndroidRuntime(27092): java.lang.NullPointerException 07-21 07:17:44.205: E/AndroidRuntime(27092): at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:632) 07-21 07:17:44.205: E/AndroidRuntime(27092): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347) 07-21 07:17:44.205: E/AndroidRuntime(27092): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296) 07-21 07:17:44.205: E/AndroidRuntime(27092): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179) 07-21 07:17:44.205: E/AndroidRuntime(27092): at in.bb.longpolling.onlineconnection.httpconnection(onlineconnection.java:89) 07-21 07:17:44.205: E/AndroidRuntime(27092): at in.bb.longpolling.onlineconnection.SGData(onlineconnection.java:39) 07-21 07:17:44.205: E/AndroidRuntime(27092): at in.bb.GActivity$6.run(GActivity.java:974) 07-21 07:17:44.205: E/AndroidRuntime(27092): at java.lang.Thread.run(Thread.java:841)
代码:
> `public String SGData(String data,int option) { //Send And Get Data
String newdataloc;
try {
newdataloc = URLEncoder.encode(data,"UTF-8");
} catch(UnsupportedEncodingException e){
Log.e("Online Connection","Error:Encoding="+e.getMessage());
}
return httpconnection(data,option);
}
private String httpconnection(String postData,int option){
Log.v("Online Connection","Get data called");
String webPage = "",resdata="";
try{
urlc = new URL(this.url);
urlConnection = (HttpURLConnection) urlc.openConnection();
Log.v("Online Connection","Connect called");
if(option==1){ //only get data
urlConnection.connect();// it gives error "already connected" if you are using it with post or get method.Uncomment and comment 'post and get method area'
}
if(option==2){
//POST METHOD AREA
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Length", Integer.toString(postData.getBytes().length));
urlConnection.setUseCaches(false);
OutputStream out = urlConnection.getOutputStream();
out.write(postData.getBytes());
out.close();
}
if(option==3){
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Content-Length", Integer.toString(postData.getBytes().length));
urlConnection.setUseCaches(false);
OutputStream out = urlConnection.getOutputStream();
out.write(postData.getBytes());
out.close();
}
InputStream is = urlConnection.getInputStream();
BufferedReader reader =new BufferedReader(new InputStreamReader(is, "UTF-8"));
Log.v("Online Connection","Bufferd called");
while ((resdata = reader.readLine()) != null){
webPage += resdata + "\n";
}
is.close();
Log.v("Online Connection","data called" + webPage);
} catch(IOException e){
Log.e("Online Connection","Error called" + e.getMessage());
}
finally {
urlConnection.disconnect();
}
return webPage;
}
calling Code:->
String mResponse=null;
do{
mResponse = online_serlst.SGData("op=3&server_name="+mServerName+"&userid="+mUserid+"&color="+DeviceCurrentPlayer+"&lr="+lr+"&ld="+pd, 2);
}while(!mResponse.toString().trim().equals("ok"));`
如果用户通过简单的单一请求点击提交按钮,我会将数据从我的游戏发送到服务器,如果有新的更新,则使用长轮询继续检查服务器的新更新.4-5分钟应用程序崩溃后使用nullpointexception。对单个请求和长轮询使用不同的线程。
在android 4.4.2 moto g设备上测试应用程序。
答案 0 :(得分:0)
根据我的理解,这可能是因为它正在关闭/终止连接池。您正在使用2个线程。一个是向服务器发送请求&其次是从服务器获取更新响应。
我认为,如果您只想更新自己的用户界面,则可以选择"服务处理程序"。当您希望频繁更新UI时,这是更新UI的最佳方式。