我如何将参数传递给AsyncTask,并将结果返回到另一个类的字段变量?

时间:2014-05-28 13:11:38

标签: java android android-asynctask google-maps-api-2

我在Android Java中遇到asynctask的问题。 这是同样的问题:variable returns null outside asynctask android 我的asynctask从网址获取数据,以获得google maps的方向! 这是我调用我的代码的地方,在onrespondre =>内myvaraible有数据,但变量之外是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);
}

这是班级:

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

2 个答案:

答案 0 :(得分:4)

我猜getter setter是非常基本的。但对于那些不知道的人:

getter setter的单独类

public class getterSetter{

String test;

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

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

Asynctask

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

并使用onResponse方法:

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

答案 1 :(得分:-1)

以下是您可以做的事情:

为您想要的变量定义一个getter - setter。 在onPostExecute方法中,设置变量的值,并在必须检索变量时,从getter获取值。 你可以为getter - setter使用一个单独的类。