android - Android 中循环中的 AsyncTask

标签 android android-asynctask android-inflate

我正在创建一个应用程序来更新城市的天气详细信息。循环将是一个城市列表。所以我希望列表中的城市尽可能多。我将使用 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;

    }

异步任务

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);

    }

}

最佳答案

与其将异步任务保持在循环中,不如将循环保持在异步任务中。并使用 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 - Android 中循环中的 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19157732/

相关文章:

android - 如何确保您不会两次启动服务android

android - 将 Android textview 绑定(bind)到 firebase 数据库字段

android - Android 2.3 以上版本中的 JSON 问题(Gingerbread)

android - 在自定义 ViewGroup 上从布局 XML 扩充内容时,布局参数不适用

java - android java 膨胀异常

android - 无法解析 android studio 中的default_web_client_id

android - 私有(private)调度TouchEvent?

android - 提供的身份验证凭据格式错误或已过期

Android 应用程序越来越慢

Android布局膨胀异常