Skip to content Skip to sidebar Skip to footer

Asynk Task Calling In Loop Not Giving The Value Return

I am try to create a application that will update the weather of multiple cities .Each row will have diffrent temp so i have use a AsynkTask. But i am not able to update the UI aft

Solution 1:

Try this

First In the for Loop

depart_arrivals_details.removeAllViews();
 for (int i = 0; i < 2; i++) {
            requestWeatherUpdate("BLR");
            }

Function WeatherResponse

private WeatherResponse requestWeatherUpdate(String location) {
        url = ""
                + location;
        Log.d("URL for Weather Upadate", url);
        WeatherUpdateAsyncTask weatherReq = new WeatherUpdateAsyncTask();
        String weatherRequestResponse="";
        try {
            weatherRequestResponse=weatherReq.execute(url).get();
            parsedWeatherResponse = ParseWeatherResponseXML
                    .parseMyTripXML(weatherRequestResponse);
        } catch (InterruptedException e) {

            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return parsedWeatherResponse;

    }

And then AsynkTask

publicclassWeatherUpdateAsyncTaskextendsAsyncTask<String, Void, String> {
    Context context;
    CallBack callBack;

   @OverrideprotectedStringdoInBackground(String... arg0) {
        String responseString = "";
        HttpClient client = null;
        try {
            client = newDefaultHttpClient();
            HttpGet get = newHttpGet(arg0[0]);
            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();

    }

    @OverrideprotectedvoidonPostExecute(String result) {
       super.onPostExecute(result);
    }

}

Solution 2:

You can update the Views that run on the UI Thread after you get the result using AsyncTask's onPostExecute(). Also, you need to call AsyncTask's execute() for the AsyncTask to actually start.

Post a Comment for "Asynk Task Calling In Loop Not Giving The Value Return"