我在活动中有以下代码:
ListView lv = (ListView) findViewById(R.id.listViewPizza2);
try {
URL url = new URL("http://api.androidhive.info/pizza/?format=xml");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setDoInput(true);
http.connect();
InputStream in = http.getInputStream();
XmlPullParserPizza2 parser_Pizza = new XmlPullParserPizza2();
pizzaList = parser_Pizza.parse(in);
BinderDataPizza bd_Pizza = new BinderDataPizza(this, pizzaHashmap);
lv.setAdapter(bd_Pizza);
}
catch (Exception e) {
e.printStackTrace();
}
如何使用AsyncTask
HttpURLConnection
来完成这项工作?
答案 0 :(得分:1)
将要执行的代码放在doInBackground
的后台。但是,您应该在UI线程上保留UI更新代码,因此请在onPostExecute
(在您调用execute()
的同一线程上安排)中调用它。
以下是转换代码的简单方法:
final ListView lv = (ListView) findViewById(R.id.listViewPizza2);
new AsyncTask<Void, Void, BinderDataPizza>() {
@Override
protected BinderDataPizza doInBackground(Void... params) {
try {
URL url = new URL("http://api.androidhive.info/pizza/?format=xml");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setDoInput(true);
http.connect();
InputStream in = http.getInputStream();
XmlPullParserPizza2 parser_Pizza = new XmlPullParserPizza2();
pizzaList = parser_Pizza.parse(in);
BinderDataPizza bd_Pizza = new BinderDataPizza(this, pizzaHashmap);
return bd_Pizza;
} catch (Exception e) {
Log.e("MyApp", "error while fetching", e);
}
return null;
}
@Override
protected void onPostExecute(BinderDataPizza bd_Pizza) {
if (bd_Pizza != null)
lv.setAdapter(bd_Pizza);
}
}.execute();
答案 1 :(得分:1)
<强> 1。创建异步任务
class GetDataAsyncTask extends AsyncTask<Void, Void, Void> {
List<Pizza> pizzaList = new ArrayList<>();
Context context;
ListView lv;
public GetDataAsyncTask(Context context, ListView lv) {
this.lv = lv;
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
try {
URL url = new URL("http://api.androidhive.info/pizza/?format=xml");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setDoInput(true);
http.connect();
InputStream in = http.getInputStream();
XmlPullParserPizza2 parser_Pizza = new XmlPullParserPizza2();
pizzaList = parser_Pizza.parse(in);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
BinderDataPizza bd_Pizza = new BinderDataPizza(this, pizzaList);
lv.setAdapter(bd_Pizza);
}
}
<强> 2。执行异步任务
ListView lv = (ListView) findViewById(R.id.listViewPizza2);
new GetDataAsyncTask(context, lv).execute();
//Use following code if you want to run multiple async tasks in parallel
new GetDataAsyncTask(context, lv).executeOnExecutors(AsyncTask.THREAD_POOL_EXECUTOR)
答案 2 :(得分:0)
您可以创建私人自定义类。
private class GetData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
}
@Override
protected Void doInBackground(Void... arg0) {
// Making a request to url and getting response
String jsonStr = makeServiceCall(your_url);//**call your http request in this method**
JSONArray contacts = null;
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(YOUR_JSON_ARRAY_TAG);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
//parse ur json object and save values in your model class
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
* Updating parsed JSON data in your UI
* */
}
}
您可以通过以下方式致电:
// Calling async task to get json
new GetData ().execute();
答案 3 :(得分:0)
private class PrefetchData extends AsyncTask<Void, Void, InputStream> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Void... arg0) {
InputStream in = null;
try {
URL url = new URL("http://api.androidhive.info/pizza/?format=xml");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setDoInput(true);
http.connect();
InputStream in = http.getInputStream();
}
catch (Exception e) {
e.printStackTrace();
}
return in;
}
@Override
public void onPostExecute(InputStream result) {
super.onPostExecute(result);
XmlPullParserPizza2 parser_Pizza = new XmlPullParserPizza2();
pizzaList = parser_Pizza.parse(in);
BinderDataPizza bd_Pizza = new BinderDataPizza(this, pizzaHashmap);
lv.setAdapter(bd_Pizza);
}
}
并以这种方式召唤它!
new PrefetchData().execute();