2013-03-26 162 views
2

如果用戶名爲&密碼正確,它必須顯示「SUCCESS」,否則必須顯示「FAILED」。我正在使用BasicNameValuePair連接到服務器。其顯示NullPointerException在這條線int code = pres.getStatusLine().getStatusCode();簡單HttpPost上的NullPointerException

public class MyPostActivity extends Activity { 
    DefaultHttpClient client; 
    HttpPost post; 
    HttpResponse res; 
    HttpEntity ent; 
    Button b; 
    List<NameValuePair> pairs; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     b = (Button) findViewById(R.id.button1); 
     client = new DefaultHttpClient(); 
     post = new HttpPost(
       "http://somesite.com/abc"); 
     b.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       List<NameValuePair> pairs = new ArrayList<NameValuePair>(3); 
       pairs.add(new BasicNameValuePair("Email", "avinash")); 
       pairs.add(new BasicNameValuePair("password", "avinash2")); 

       try { 
        post.setEntity(new UrlEncodedFormEntity(pairs)); 
       } catch (UnsupportedEncodingException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       HttpResponse pres = null; 
       try { 
        pres = client.execute(post); 
       } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       int code = pres.getStatusLine().getStatusCode(); 
       if (code == 200) { 
        Toast.makeText(getApplicationContext(), "Successful", 
          Toast.LENGTH_SHORT).show(); 
       } else { 
        Toast.makeText(getApplicationContext(), "Failed!", 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 

    } 

} 

enter image description here

+0

認沽Web服務文件還 – 2013-03-26 11:18:07

+0

你的意思是鏈接? – DroidLearner 2013-03-26 11:18:54

+0

請分享logcat ...檢查您是否在撥打'執行'除了'執行' – 2013-03-26 11:19:08

回答

4

你應該Internet權限添加到AndroidManifest.xml文件。您也不應該在UI Thread上執行阻止操作。實際上,Http execute將導致UI Thread掛起等待服務器的響應。這將導致ANR(應用程序沒有響應)。我建議你閱讀以下article

+0

好吧..我會做..這裏是我的清單http://pastebin.com/VUnG1PEr – DroidLearner 2013-03-26 11:27:33

+0

+1爲確切的原因 – Pragnani 2013-03-26 11:29:24

0
 try { 
      pres = client.execute(post); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     int code = pres.getStatusLine().getStatusCode(); 

井空指針異常情況發生,因爲你可能在你的client.execute線得到一個例外,注意做這件事(你只是抓住它)。這就是爲什麼pres爲空。你應該打印異常來找到你真正的問題。

+0

如何打印異常? – DroidLearner 2013-03-26 11:32:03

1

也請添加此權限您的清單,並嘗試

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > 
    </uses-permission> 

而且代碼的AsyncTask

private class SignInService extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     try { 
      progressDilaog = ProgressDialog.show(MainActivity.this, "", 
        "Loading", true, false); 

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

    @Override 
    protected Void doInBackground(Void... params) { 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(url); 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5); 
     nameValuePairs.add(new BasicNameValuePair("username", "abc")); 
     nameValuePairs.add(new BasicNameValuePair("password", "bcd")); 

     try { 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      try { 
       HttpResponse response = httpclient.execute(httppost); 

       int responsecode = response.getStatusLine().getStatusCode(); 


      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     progressDilaog.dismiss(); 


    } 
} 
+0

到目前爲止沒有錯誤...並且它沒有顯示吐司..'int responsecode = response.getStatusLine()。getStatusCode(); \t \t \t \t \t如果(responsecode == 200){ \t \t \t \t \t \t Toast.makeText(getApplicationContext(), 「成功」, \t \t \t \t \t \t \t \t Toast.LENGTH_SHORT)。顯示(); \t \t \t \t \t}否則{ \t \t \t \t \t \t Toast.makeText(getApplicationContext(), 「失敗」。, \t \t \t \t \t \t \t \t Toast.LENGTH_SHORT).show(); \t \t \t \t \t}' – DroidLearner 2013-03-26 12:16:38

+1

你不能在doInBackground中使用使用Toast。如果您想調試,請使用Log.d()。 – greenapps 2013-03-26 17:43:13

相關問題