2012-08-02 49 views
0

在執行下面的代碼時,我在關閉流時在try塊中的類LoginAuthenticate中收到NullPointerException。NULL指針異常HTTP連接

我正在使用一個AsyncTask,並從按鈕上的onClick激活它。

onClick我打電話給登錄身份驗證,並在嘗試塊獲取異常。

public class DattabActivity extends Activity implements View.OnClickListener { 
    /** Called when the activity is first created. */  
    private final static String SERVICE_URI = "http://182.50.154.23/Dattab.Device.Rest/ServiceClass/RestDeviceService.svc"; 

     @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.v("DEBUG", "DattabActivity");   
     setContentView(R.layout.login); 
     Button loginbutton=(Button)findViewById(R.id.loginbtn); 
     loginbutton.setOnClickListener(this); 

     Intent available=new Intent(); 
     available.setClass(DattabActivity.this, Available_Surveys.class);   

    } 

    public void onClick(View v) { 
     switch(v.getId()){ 

     case R.id.loginbtn: 
      // TODO Auto-generated method stub 
      EditText userid = (EditText)findViewById(R.id.useredt); 
      EditText passwd = (EditText)findViewById(R.id.pwdedt); 

      Log.i("DEBUG", "DattabActivity Click Button "); 

      userid.setText("surveyor1"); 
      passwd.setText("dev"); 
      if(userid.getText().toString()=="" ||passwd.getText().toString()==""){ 

       Toast.makeText(getApplicationContext(), "Please enter your account details", Toast.LENGTH_LONG).show(); 
      } 
      else{ 
        String params = SERVICE_URI + "/authenticateuser/"+userid.getText().toString()+"/"+passwd.getText().toString(); 
        try { 
          LoginAuthenticate la = new LoginAuthenticate(); 
          la.execute(params); 
          la.get(); 


         JSONObject json = new JSONObject(new String(la.getStreamBuffer())); 
         Intent available=new Intent(); 
         available.setClass(DattabActivity.this, Available_Surveys.class); 
         available.putExtra("userid", json.getString("SurveyorId")); 
         startActivity(available); 
         Toast.makeText(getApplicationContext(), json.getString("SurveyorId"),Toast.LENGTH_LONG).show(); 
         Toast.makeText(getApplicationContext(), "Login Clicked", Toast.LENGTH_LONG).show(); 
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        catch(Exception e){ 
         e.printStackTrace(); 
        } 



      } 

      break ; 
     } 

    } 
    private class LoginAuthenticate extends AsyncTask<String,Integer,char[]> { 
     private char[] streamBuffer ; 
     @Override 
     protected char[] doInBackground(String... params) { 
      android.os.Debug.waitForDebugger(); 

      String url=params[0]; 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(url); 
      try { 
        request.setHeader("Accept", "application/json"); 
        request.setHeader("Content-type", "application/json"); 
        HttpResponse response = httpClient.execute(request); 
        HttpEntity responseEntity = response.getEntity(); 

        // Read response data into buffer 
        char[] buffer = new char[(int)responseEntity.getContentLength()]; 
        InputStream stream = responseEntity.getContent(); 
        InputStreamReader reader = new InputStreamReader(stream); 
        reader.read(buffer); 
        stream.close();//here it goes in catch NULL Pointer 
        return buffer ; 

      }catch(Exception e){ 
       e.printStackTrace(); 
       return null; 
      } 

     } 
     protected void onPostExecute(char[] result){ 
      streamBuffer = result ; 
     } 

     public char[] getStreamBuffer(){ 
      return streamBuffer ; 
     } 
    } 
} 
+0

您是否在Manifest.xml中提供了Internet權限 – VenomVendor 2012-08-02 09:28:11

+0

嘗試檢查Log.e(「」,「buffer:」+ buffer)進入緩衝區的內容。 這將幫助您確定服務器端是否存在某些錯誤。 – 2012-08-02 09:56:02

回答

0

試試這個來處理null異常錯誤。

 try { 
       request.setHeader("Accept", "application/json"); 
       request.setHeader("Content-type", "application/json"); 
       HttpResponse response = httpClient.execute(request); 
       HttpEntity responseEntity = response.getEntity(); 

       // Read response data into buffer 
       char[] buffer = new char[(int)responseEntity.getContentLength()]; 
       try{ 
         InputStream stream = responseEntity.getContent(); 
         InputStreamReader reader = new InputStreamReader(stream); 
         reader.read(buffer); 
       } 
       finally{ 
         stream.close();//here it goes in catch NULL Pointer 
        } 
       return buffer ; 

     }catch(Exception e){ 
      e.printStackTrace(); 
      return null; 
     } 

鏈接:https://stackoverflow.com/a/156889/1503155

+0

其實我總是無法理解我在doInbackGround中做了什麼錯誤我正在使用stream和onPostExecute我正在更新它並通過getStreamBuffer()我正嘗試在UI線程上使用它來更新UI,但每當它進入在try塊中關閉流時捕獲並重新調用NULL,所以每次拋出NULL指針異常所以基本上每次都要捕獲塊。 – 2012-08-02 08:04:53

0

有很多原因,HTTP返回空指針異常。 您的互聯網不工作, 您正在訪問的URL不存在或不存在。 在你的try catch塊中,你正在做的錯誤是導致異常,你的catch塊捕獲每個異常總是返回空指針。

如果您在模擬器上進行測試,而且您確定該URL是正確的,並且代碼沒有任何問題,則可以取消/啓用Internet連接,清理項目並重新構建,然後重新啓動模擬器。 有時它修復了仿真器中的Internet問題。