Search code examples
javaandroidandroid-asynctaskgoogle-maps-api-2

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

Solution

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