[英]How to make Apache HTTP request from AsyncTask
I am trying to get json
values using HTTP
POST
method. 我正在尝试使用
HTTP
POST
方法获取json
值。 So far I am able to receive values with GET
method. 到目前为止,我已经能够使用
GET
方法接收值。 And here is the code so far: 这是到目前为止的代码:
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);
}
}
Now assume if I have a field number
with value= +919061037828
, how do I adapt my code to get result in POST method? 现在假设我有一个值为=
+919061037828
的字段number
,如何修改我的代码以获取POST方法中的结果?
This is how I call my AsyncTask
: 这就是我所说的
AsyncTask
:
public static final String URL = "https://api.eduknow.info/mobile/get_details";
new SimpleTask().execute(URL);
I guess your problem is with parameters. 我猜你的问题是参数。
Try sth like this: 尝试这样:
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);
Hope it will help. 希望它会有所帮助。
EDIT And I just found another possible solution for your problem: Here 编辑我刚刚找到了您的问题的另一个可能的解决方案: 这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.