2014-07-21 72 views
0

我正在使用httpurlconnection测试Android中的长轮询,但应用程序仍然因错误nullpointerexception而崩溃。httpurlconnection getInputStream中的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.using不同的线程为单个请求和长轮询。

在android 4.4.2 moto g设备上测试应用程序。

+0

哪一行是39? – VVB

+0

以上代码中的SGdata方法 public String SGData(String data,int option){........... return httpconnection(data,option); } – Kanishk

回答

0

根据我的理解,这可能是因为它关闭/终止连接池。您正在使用2个线程。一种是向服务器发送请求&其二是从服务器获取更新响应。

我想,如果你只是想更新你的用户界面,你可以更喜欢“服务处理程序”。当您想要频繁更新UI时,这是更新UI的最佳方式。

相关问题