首页 文章

getInputStream中的httpurlconnection NullPointerException

提问于
浏览
0

我正在使用httpurlconnection测试android中的长轮询,但是应用程序会因错误nullpointerexception而崩溃 .

07-21 07:17:42.469:V /在线连接(27092):缓冲区称为07-21 07:17:42.469:V /在线连接(27092):数据称为07-21 07:17:42.471:V /在线连接(27092):获取数据称为07-21 07:17:42.471:E /在线连接(27092):错误称为已关闭07-21 07:17:42.472:V /在线连接(27092):获取名为07的数据-21 07:17:42.473:V /在线连接(27092):连接称为07-21 07:17:42.479:V /在线连接(27092):连接称为07-21 07:17:42.832:V /在线连接(27092):Bufferd,名为07-21 07:17:42.832:V / Online Connection(27092):data calledok 07-21 07:17:44.147:V / Online Connection(27092):Bufferd,名为07-21 07:17 :44.147:V /在线连接(27092):数据称为07-21 07:17:44.148:V /在线连接(27092):获取数据称为07-21 07:17:44.149:V /在线连接(27092):连接叫做07-21 07:17:44.152:电子邮件/在线连接(27092):错误名为Socket关闭07-21 07:17:44.205:E / AndroidRuntime(27092):致命异常:Thread-34364 07-21 07:17 :44.205:E / A. ndroidRuntime(27092):进程: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.bb.G活动$ 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设备上测试应用程序 .

1 回答

  • 0

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

    我想,如果你只想更新你的用户界面,你可以选择“服务处理程序” . 当您希望频繁更新UI时,这是更新UI的最佳方法 .

相关问题