Java 如何将参数传递给AsyncTask,并将结果从另一个类返回给字段变量?
我在android Java中遇到了Java 如何将参数传递给AsyncTask,并将结果从另一个类返回给字段变量?,java,android,android-asynctask,google-maps-api-2,Java,Android,Android Asynctask,Google Maps Api 2,我在android Java中遇到了asynctask问题。 与此问题相同: 我的asynctask从url获取数据,以获取谷歌地图的方向! 这就是我调用代码的地方,在onrespondre=>myvaraible内部有数据,但在变量外部是null public void onResponse(String status, Document doc, final GoogleDirection gd) { mMap.addMarker(new MarkerOptions()
asynctask
问题。
与此问题相同:
我的asynctask从url获取数据,以获取谷歌地图的方向!
这就是我调用代码的地方,在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();
}
}
私有类RequestTask扩展了AsyncTask{
受保护文档doInBackground(字符串…url){
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpContext localContext=新的BasicHttpContext();
HttpPost HttpPost=新的HttpPost(url[0]);
HttpResponse response=httpClient.execute(httpPost,
本地上下文);
InputStream in=response.getEntity().getContent();
DocumentBuilder=DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
返回builder.parse(in);
}捕获(IOE异常){
e、 printStackTrace();
}捕获(ParserConfiguration异常e){
e、 printStackTrace();
}捕获(SAXE异常){
e、 printStackTrace();
}
返回null;
}
后期执行时受保护的无效(文档文档){
super.onPostExecute(doc);
if(mDirectionListener!=null){
mDirectionListener.onResponse(getStatus(doc)、doc、,
谷歌(GoogleDirection.this);
}
}
私有字符串getStatus(文档文档){
NodeList nl1=doc.getElementsByTagName(“状态”);
节点node1=nl1。项(0);
如果(isLogging){
Log.i(“谷歌方向”,“状态:+node1.getTextContent());
}
返回node1.getTextContent();
}
}
以下是您可以做的:
为所需的变量定义一个getter-setter。
在onPostExecute方法中,设置变量的值,当必须检索它时,从getter获取值。
您可以为getter-setter使用单独的类。我想getter-setter
是非常基本的。但对于那些不知道的人来说:
getter setter的单独类
public class getterSetter{
String test;
public getterSetter()
{
super();
test= "";
}
public String getStatus()
{
return test;
}
public setStatus(String input)
{
test= input;
}
}
在异步任务中
:
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);
}
什么是空的?你能更具体一点吗?例如,我声明了一个对象:Integer test;当我影响变量测试getDistanceValue时,我的变量测试现在有一个数据(假设是8),但如果我在asynctask之外,我的变量测试为null(无数据)变量将保持空值,直到为变量赋值的线程finishes@IllegalArgument我怎么知道线程完成需要多少时间?因为如果我不解决这个问题,我就无法在我的字段变量中获得距离或持续时间。在AsyncTask的onpostexecute方法中,如果你真的需要帮助社区,你应该提供一个示例或一些代码片段。当你发布答案时,你应该记住,如果你已经发布了答案,那么应该编辑你的答案,而不是发布第二个答案。享受@@PiyushGupta:编辑时,请注意内联代码跨越(,如此
),仅用于句子中的代码。此外,请尝试和改进后尽可能多的编辑时,因为职位是颠簸的过程。谢谢