Android将代码更改为AsyncTask
我在活动中有以下代码:Android将代码更改为AsyncTask,android,android-asynctask,Android,Android Asynctask,我在活动中有以下代码: 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.setDo
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();
}
如何使用
HttpURLConnection
的AsyncTask
执行此操作?将要在后台执行的代码放到doInBackground
中。但是,您应该将UI更新代码保留在UI线程上,因此在onPostExecute
中调用它(它安排在您调用execute()
on的同一线程上)
以下是转换代码的简单方法:
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();
final ListView lv=(ListView)findViewById(R.id.listViewPizza2);
新建异步任务(){
@凌驾
受保护的BinderDataBackground(无效…参数){
试一试{
URL=新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=新的XmlPullParserPizza2();
pizzaList=parser\u Pizza.parse(in);
BinderDataPizza bd_Pizza=新BinderDataPizza(这是PizzahasMap);
返回bd_比萨饼;
}捕获(例外e){
Log.e(“MyApp”,“抓取时出错”,e);
}
返回null;
}
@凌驾
PostExecute上受保护的void(BinderDataPizza bd_Pizza){
if(bd_Pizza!=null)
lv.设置适配器(bd_Pizza);
}
}.execute();
将要在后台执行的代码放到doInBackground
中。但是,您应该将UI更新代码保留在UI线程上,因此在onPostExecute
中调用它(它安排在您调用execute()
on的同一线程上)
以下是转换代码的简单方法:
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();
final ListView lv=(ListView)findViewById(R.id.listViewPizza2);
新建异步任务(){
@凌驾
受保护的BinderDataBackground(无效…参数){
试一试{
URL=新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=新的XmlPullParserPizza2();
pizzaList=parser\u Pizza.parse(in);
BinderDataPizza bd_Pizza=新BinderDataPizza(这是PizzahasMap);
返回bd_比萨饼;
}捕获(例外e){
Log.e(“MyApp”,“抓取时出错”,e);
}
返回null;
}
@凌驾
PostExecute上受保护的void(BinderDataPizza bd_Pizza){
if(bd_Pizza!=null)
lv.设置适配器(bd_Pizza);
}
}.execute();
您可以创建一个私有自定义类
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
* */
}
}
您可以创建一个私有自定义类
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
* */
}
}
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);
}
}
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)
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);
}
}
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)