【问题标题】:How I can pass arguments to AsyncTask, and returning results to a field variable from another class?如何将参数传递给 AsyncTask,并将结果返回到另一个类的字段变量?
【发布时间】:2014-05-28 13:11:37
【问题描述】:

我在 android Java 中遇到了 asynctask 的问题。 这是同样的问题:variable returns null outside asynctask android 我的 asynctask 从 url 获取数据,以获取 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();
    }
}

【问题讨论】:

  • 什么是空?你能说得更具体点吗?
  • 例如,我声明一个对象:Integer test;当我影响我的变量测试 getDistanceValue 时,我的变量测试现在有一个数据(假设它是 8)但是如果我不在 asynctask 之外,我的变量测试为空(无数据)
  • 你的变量将保持为空,直到你的变量被赋值的线程结束
  • @IllegalArgument 我怎么知道线程需要多长时间才能完成?因为如果我不解决这个问题,我就无法在我的字段变量中获得距离或持续时间。
  • 在你的 asynctask 的 onpostexecute 方法中

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


【解决方案1】:

我猜getter setters 是非常基础的。但对于不知道的人:

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

【讨论】:

  • @PiyushGupta:编辑时,请注意内联代码跨度(like thisshouldn't be used for highlighting,仅适用于句子中的代码。另外,请在编辑时尝试尽可能地改进帖子,因为在此过程中会碰到帖子。谢谢!
【解决方案2】:

你可以这样做:

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

【讨论】:

  • 如果你真的需要帮助社区,你应该提供一个例子或一些代码sn-p。
  • 当您发布答案时,您应该记住,如果您已经发布了答案,那么您应该编辑您的答案,而不是发布第二个答案。享受!!@
猜你喜欢
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
  • 2022-01-05
相关资源
最近更新 更多