2015-12-24 42 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