2014-03-13 449 views
0
try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://nayyar.5gbfree.com/welcome.php"); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 

       Log.e("log_tag", "connection success "); 
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); 
      } 

我試圖第一次連接到服務器。我從互聯網複製代碼嘗試執行。但總是在nameValuePairs上顯示錯誤。我不知道這是什麼。以及爲什麼錯誤是。有人向我解釋代碼和錯誤,或者解釋我通過代碼以他/她自己的方式連接到服務器。應該非常感激。連接到服務器http客戶端

+3

不,我們不會給你code_。當你用你寫的代碼發佈問題時,**總是**陳述你的目標。如果有錯誤,**總是**發佈堆棧跟蹤或編譯器錯誤。或者解釋一下你的期望如何表現。 –

+0

這意味着,你應該聲明那些nameValuePairs! –

+0

錯誤:nameValuePairs無法解析爲變量 –

回答

0

你必須做出的NameValuePair列表...這樣

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://nayyar.5gbfree.com/welcome.php"); 
List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
        postParameters); 
httppost.setEntity(formEntity); 
HttpResponse response = httpclient.execute(httppost); 

,然後得到這樣

BufferedReader br = new BufferedReader(new InputStreamReader(
        httppost.getEntity().getContent())); 

br.readLine(); // Save it in a String variable or whatever you want 
0

哪些數據必須得到您的POST數據?例如,如果url需要「id」和「user」:

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://nayyar.5gbfree.com/welcome.php"); 

      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
      nameValuePairs.add(new BasicNameValuePair("user", "Jorgesys!")); 

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    ... 
    ... 
    ... 
相關問題