AndroLife
AndroLife

Reputation: 948

How I can pass arguments to AsyncTask, and returning results to a field variable from another class?

I have a problem with asynctask in android Java. it's the same problem as this : variable returns null outside asynctask android my asynctask get data from a url , to get direction of google maps ! This is where I call my code , inside onrespondre => myvaraible have data but outside the variable is null.

    public void onResponse(String status, Document doc, final GoogleDirection gd) {
    mMap.addMarker(new MarkerOptions()
            .position(
                    new LatLng(myLocation.getLatitude(), myLocation
                            .getLongitude()))
            .icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
    mMap.addMarker(new MarkerOptions().position(Position).icon(
            BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_RED)));

    mMap.addPolyline(gd.getPolyline(doc, 3, Color.RED));
    MyVariable = gd.getTotalDurationValue(doc);
}

this is the class :

    private class RequestTask extends AsyncTask<String, Void, Document> {
    protected Document doInBackground(String... url) {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url[0]);
            HttpResponse response = httpClient.execute(httpPost,
                    localContext);
            InputStream in = response.getEntity().getContent();
            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();

            return builder.parse(in);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(Document doc) {
        super.onPostExecute(doc);

        if (mDirectionListener != null) {
            mDirectionListener.onResponse(getStatus(doc), doc,
                    GoogleDirection.this);
        }
    }

    private String getStatus(Document doc) {
        NodeList nl1 = doc.getElementsByTagName("status");
        Node node1 = nl1.item(0);

        if (isLogging) {
            Log.i("GoogleDirection", "Status : " + node1.getTextContent());
        }

        return node1.getTextContent();
    }
}

Upvotes: 3

Views: 153

Answers (2)

Preeti Sakhare
Preeti Sakhare

Reputation: 56

I guess getter setters are very basic. But for people who don't know:

A separate class for the getter setter

public class getterSetter{

String test;

public getterSetter()
{
    super();
    test= "";
}

public String getStatus()
{
    return test;
}
public setStatus(String input)
{
    test= input;
}
}

In your Asynctask:

protected void onPostExecute(Document doc) {
        super.onPostExecute(doc);
        setStatus(getStatus(doc));
        if (mDirectionListener != null) {
            mDirectionListener.onResponse(doc,
                    GoogleDirection.this);
        }

And in your onResponse method:

public void onResponse(Document doc, final GoogleDirection gd) {

String status = getStatus();
mMap.addMarker(new MarkerOptions()
        .position(
                new LatLng(myLocation.getLatitude(), myLocation
                        .getLongitude()))
        .icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
mMap.addMarker(new MarkerOptions().position(Position).icon(
        BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_RED)));

mMap.addPolyline(gd.getPolyline(doc, 3, Color.RED));
MyVariable = gd.getTotalDurationValue(doc);
}

Upvotes: 4

Preeti Sakhare
Preeti Sakhare

Reputation: 56

Here is what you can do:

Define a getter - setter for the variable that you want. In your onPostExecute method, set the value of your variable and when you have to retrieve it, get the value from the getter. You may use a separate class for the getter - setter.

Upvotes: -1

Related Questions