异步HTTP发布的android [英] Async HTTP post android
问题描述
我是用在哪里,我只是需要从一个MySQL服务器下载一对坐标的谷歌地图的应用。我能做到这一点成功地使用PHP和正常httpost但我的应用程序冻结的时候我做了几秒钟。
I am using an application where i just need to download a pair of coordinates for google maps from a mysql server. I can do this successfully using php and a normal httpost but my app freezes for a few seconds when i do it.
我读了你需要httppost asycronous,以避免冻结,直到该服务器完成的prossess并将结果发送。
I read that you need to make httppost asycronous in order to avoid that freezing until the server finishes the prossess and sends the results.
的一点是,我需要的结果的一个JSON数组像正常httpost。
The point is that i need that results in a json array like the normal httpost.
例如,如果我有这个httppost。
For example if i had this httppost.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "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
}
String result11 = null;
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is11, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is11.close();
result11 = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// parsing data
try {
JSONArray jArray1 = new JSONArray(result11);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我如何可以转换到异步后这样我就可以避开冻结?
How can i convert that to an asynchronous post so i can avoid the freeze ?
推荐答案
有一个很好的类的AsyncTask
可用于轻松运行异步任务。如果嵌入您的code在这样的子类,你可能最终得到这样的:
There is a nice class AsyncTask
which can be used to easily run asynchronous tasks. If you embed your code in such a subclass, you might end up with this:
public class FetchTask extends AsyncTask<Void, Void, JSONArray> {
@Override
protected JSONArray doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
reader.close();
String result11 = sb.toString();
// parsing data
return new JSONArray(result11);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(JSONArray result) {
if (result != null) {
// do something
} else {
// error occured
}
}
}
您可以使用启动任务:
new FetchTask().execute();
其他资源:
- Painless Threading - developer.android.com
- AsyncTask - developer.android.com
这篇关于异步HTTP发布的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!