2014-02-28 195 views
0

這是我的Java代碼:POST請求。 Android的

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

    case R.id.button1: 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://iwindroids.ru/test.php"); 

     try { 
      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
      //nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
      nameValuePairs.add(new BasicNameValuePair("lal", "AndDev is Cool!")); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 

     } catch (ClientProtocolException e) { 

      // TODO Auto-generated catch block 
     } catch (IOException e) { 

      // TODO Auto-generated catch block 
     } 
    break; 
    default: 
    break; 

    } 
} 

當我按下按鈕1寫道:

不幸的是,[應用名稱]應用已經停止。

我在Manifest中寫道。

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

Android。蝕。 POST。 Http

+1

搜索'NetworkOnMainThreadException'。 – Raghunandan

+0

你這樣做到背景線程。 –

+0

您不捕捉一般例外...使用它你會知道什麼是問題 –

回答

1

你不應該從main/UI線程進行http調用。你應該在android的一個單獨的線程中做。因此,使用新的Thread或AsyncTask進行http調用並稍後更新UI

+0

正確,但我會建議一個AsyncTask,因爲它給你更多的UI控制'onPreExecute ','onProgressUpdate','onPostExecute'方法。 –

0

創建一個AsyncTask類,如下所示。並打電話給你想要的AsyncTask類 。在的AsyncTask類調用Web服務在doInBackground方法

class asyncTaskPost extends AsyncTask<String, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 

     } 

     @Override 
     protected Void doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      // call the webservice post method here 
      postMethod(); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 

     } 
    } 

的PostMethod 而你的Web服務POST方法如下面

public static void postMethod() 
    { 

    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("place your url here"); 
     try { 



      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("name", name)); 
      nameValuePairs.add(new BasicNameValuePair("phoneno", strMobileNumber)); 

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      // Execute HTTP Post Request 
      HttpResponse response= httpclient.execute(httppost); 
      HttpEntity responseEntity = response.getEntity(); 
      String serverResponse = EntityUtils.toString(responseEntity); 
      Log.i("server response is="+serverResponse,""); 

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

      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 
    } 
+0

---->'保護無效doInBackground(字符串... PARAMS){' 在這條線 \t多個標記 - 實現 \t android.os.AsyncTask .doInBackground \t - 此方法必須返回類型爲Void的結果 – Aydomir

+0

我放棄在該方法中添加return語句,現在檢查一個@Aydomir –