我正在膨胀的XML文件
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/depart_details" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" > <ImageVIEw androID:ID="@+ID/flight_depart_image" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" androID:padding="3dip" androID:layout_margintop="10dp" androID:src="@drawable/f1" /> <TextVIEw androID:ID="@+ID/depart_time" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="10dp" androID:layout_margintop="5dp" androID:layout_toRightOf="@+ID/flight_depart_image" androID:text="" androID:textcolor="#666666" androID:textSize="25sp" androID:textStyle="bold" /> <TextVIEw androID:ID="@+ID/depart_airport_city" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="10dp" androID:layout_margintop="5dp" androID:layout_toRightOf="@+ID/depart_time" androID:text="" androID:textcolor="#666666" androID:textSize="15sp" androID:textStyle="bold" /> <TextVIEw androID:ID="@+ID/depart_airport" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@+ID/depart_airport_city" androID:layout_marginleft="125dp" androID:text="N/A" androID:textcolor="#666666" androID:textSize="12sp" androID:textStyle="bold" /> <ImageVIEw androID:ID="@+ID/weather_image" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft ="40dp" androID:layout_toRightOf="@+ID/depart_airport" androID:src="@drawable/image" /> <TextVIEw androID:ID="@+ID/tempraturetext" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="3dp" androID:layout_toRightOf="@+ID/weather_image" androID:text="Temp:" /> <TextVIEw androID:ID="@+ID/temprature" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="3dp" androID:layout_toRightOf="@+ID/tempraturetext" androID:text="20℃" /> <TextVIEw androID:ID="@+ID/humIDity_text" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@+ID/temprature" androID:layout_marginleft="3dp" androID:layout_toRightOf="@+ID/weather_image" androID:text="HumIDity:" /> <TextVIEw androID:ID="@+ID/humIDity" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@+ID/temprature" androID:layout_marginleft="3dp" androID:layout_toRightOf="@+ID/humIDity_text" androID:text="32" /> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="1dp" androID:layout_below="@+ID/depart_airport" androID:layout_margintop="5dp" androID:background="#d3d3d3" > </linearLayout>
我创建的这个函数发送请求
private WeatherResponse requestWeatherUpdate(String location) { url = "" + location; //This location will be dyanamic and multiple Log.d("URL for Weather Upadate",url); WeatherUpdate weatherReq = new WeatherUpdate(new CallBack() { @OverrIDe public voID run(Object result) { try { AppResponse = (String) result; response = ParseWeatherResponseXML .parseMyTripXML(AppResponse); } catch (Exception e) { Log.e("TAG Exception Occured","Exception is " + e.getMessage()); } } }); weatherReq.execute(url); return response; }
AsynkTask
public class WeatherUpdate extends AsyncTask<String,VoID,String> { Context context; CallBack callBack; public WeatherUpdate(CallBack callBack) { this.callBack = callBack; } @OverrIDe protected String doInBackground(String... arg0) { String responseString = ""; httpClIEnt clIEnt = null; try { clIEnt = new DefaulthttpClIEnt(); httpGet get = new httpGet(IweenTripDetails.url); clIEnt.getParams().setParameter("http.socket.timeout",6000); clIEnt.getParams().setParameter("http.connection.timeout",6000); httpResponse responseGet = clIEnt.execute(get); httpentity resEntityGet = responseGet.getEntity(); if (resEntityGet != null) { responseString = EntityUtils.toString(resEntityGet); Log.i("GET RESPONSE",responseString.trim()); } } catch (Exception e) { Log.d("ANDRO_ASYNC_ERROR","Error is " + e.toString()); } Log.d("ANDRO_ASYNC_RESPONSE",responseString.trim()); clIEnt.getConnectionManager().shutdown(); return responseString.trim(); } @OverrIDe protected voID onPostExecute(String result) { // Todo auto-generated method stub super.onPostExecute(result); callBack.run(result); }}解决方法 而不是将AsynChronous任务保持在循环中,将循环保持在AsynChronous中.并使用onProgressUpdate更新UI.
更新了示例代码
public class Asyn extends AsyncTask<String,String,String> { @OverrIDe protected String doInBackground(String... params) { for (String location : params) { String tmp = getTemp(location); publishProgress(tmp); /** Use Result **/ } return null; } /** * Calculate Weather * * @param location * @return */ private String getTemp(String location) { return location; } @OverrIDe protected voID onProgressUpdate(String... values) { super.onProgressUpdate(values); Log.e("Temo",values[0]); }}
并传递您开始处理的位置数组
Asyn m = new Asyn(); String arrryaLocation = null; // your vales m.execute(arrryaLocation);总结
以上是内存溢出为你收集整理的Android中的AsyncTask循环中的AsyncTask全部内容,希望文章能够帮你解决Android中的AsyncTask循环中的AsyncTask所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)