2015-12-24 72 views
0

我想獲得json值使用HTTPPOST方法。到目前爲止,我能夠通過GET方法接收值。這裏是到目前爲止的代碼:如何使從AsyncTask的Apache HTTP請求

private class SimpleTask extends AsyncTask<String, Void, String> { 

    @Override 
    protected void onPreExecute() { 
     // Create Show ProgressBar 
    } 

    protected String doInBackground(String... urls) { 
     String result = ""; 
     try { 

      HttpGet httpGet = new HttpGet(urls[0]); 
      HttpClient client = new DefaultHttpClient(); 

      HttpResponse response = client.execute(httpGet); 

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

      if (statusCode == 200) { 
       InputStream inputStream = response.getEntity().getContent(); 
       BufferedReader reader = new BufferedReader 
         (new InputStreamReader(inputStream)); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        result += line; 
       } 
      } 

     } catch (ClientProtocolException e) { 

     } catch (IOException e) { 

     } 
     //Log.w("PREMIERE::::",result); 
     return result; 
    } 

    protected void onPostExecute(String jsonString) { 
     // Dismiss ProgressBar 
     showData(jsonString); 
    } 
} 

現在假設,如果我有一個字段number與價值= +919061037828,我該如何適應即可導致POST方法我的代碼?

這是我打電話給我的AsyncTask

public static final String URL = "https://api.eduknow.info/mobile/get_details"; 
new SimpleTask().execute(URL); 

回答

1

我猜你的問題是與參數。

嘗試某事像這樣:

HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost("your url"); 
     List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); 

     nameValuePair.add(new BasicNameValuePair("username", "username")); 
     nameValuePair.add(new BasicNameValuePair("lang", "en")); 

     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
     UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePair); 
     HttpResponse response = httpClient.execute(httpPost); 

希望這將有助於。

編輯 我只是找到了你的問題的另一種可能的解決方案: Here