[英]How I can pass arguments to AsyncTask, and returning results to a field variable from another class?
我在android Java中的asynctask
有问题。 这是同样的问题: 变量在asynctask android外返回null我的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();
}
}
我猜getter setter
是非常基本的。 但对于不知道的人:
getter设置程序的单独类
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);
}
您可以执行以下操作:
为所需的变量定义一个getter-setter。 在onPostExecute方法中,设置变量的值,然后在必须检索它时,从getter获取值。 您可以为getter-setter使用单独的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.