Android中的AsyncTask循环中的AsyncTask

Android中的AsyncTask循环中的AsyncTask,第1张

概述我正在创建一个应用程序,它将更新城市的天气详细信息.循环将是一个城市列表.所以我想要列表中的城市.我将使用AsyncTask方法发送请求和解析它.同时我正在给布局充气,我必须在布局中放置与城市相对应的所有天气细节.如何实现这一点请帮助我. 我正在膨胀的XML文件 <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android 我正在创建一个应用程序,它将更新城市的天气详细信息.循环将是一个城市列表.所以我想要列表中的城市.我将使用AsyncTask方法发送请求和解析它.同时我正在给布局充气,我必须在布局中放置与城市相对应的所有天气细节.如何实现这一点请帮助我.

我正在膨胀的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所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1129953.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存