gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:50:26 25 4
gpt4 key购买 nike

我在 android Java 中遇到 asynctask 问题。这是同样的问题:variable returns null outside asynctask android我的异步任务从 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();
}
}

最佳答案

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

关于java - 我如何将参数传递给 AsyncTask,并将结果返回给另一个类的字段变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23912590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com