gpt4 book ai didi

android - 如何从异步任务返回数据

转载 作者:行者123 更新时间:2023-11-29 16:13:51 25 4
gpt4 key购买 nike

我有一个扩展异步任务的类

private class taskMK extends AsyncTask<Void,Void,JSONObject>{
String url;
JSONObject json;
public taskMK(String tr){
this.url = tr;
}
@Override
protected JSONObject doInBackground(Void... params) {
JSONParser jsonParser = new JSONParser();
JSONObject json = jsonParser.getJSONFromURL(url);
return json;
}

@Override
protected void onPostExecute(JSONObject result) {
this.json = result;
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
public JSONObject getkq(){
return this.json;
}

}

Activity 中

String url = "https://maps.googleapis.com/maps/api/place/search/json?location="+lat+"%2C"+lng+"&name=atm&radius="+bk+"&sensor=false&key=AIzaSyCxaZYo1zJ_QxuNcp5dL5P0xm5XvIJPXRw";
taskMK mk = new taskMK(url);
mk.execute();
JSONObject json = mk.getkq();

我打算将这段代码放在 asynctask 类中,但出现错误“无法在未调用 Looper.prepare() 的线程内创建处理程序”....实际上,我不知道如何解决这个问题.

    Double lat = loc.getLatitude();
Double lng = loc.getLongitude();
String url = "https://maps.googleapis.com/maps/api/place/search/json?location="+lat+"%2C"+lng+"&name=atm&radius="+bk+"&sensor=false&key=AIzaSyCxaZYo1zJ_QxuNcp5dL5P0xm5XvIJPXRw";

JSONParser jsonParser = new JSONParser();
JSONObject json = jsonParser.getJSONFromURL(url);
marker=getResources().getDrawable(R.drawable.hoe);
itemizedOverlay = new MarkerATM(marker, mapView);
int i=0;
try{
JSONArray array_atm = json.getJSONArray("results");
for(i=0;i<array_atm.length();i++){
JSONObject c = array_atm.getJSONObject(i);
String atm_name = c.getString("name");
String atm_address = c.getString("vicinity");

JSONObject geometry = c.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
String atm_lat = location.getString("lat");
String atm_lng = location.getString("lng");

Double _lat = Double.parseDouble(atm_lat)*1E6;
Double _lng = Double.parseDouble(atm_lng)*1E6;

GeoPoint myPoint = new GeoPoint(_lat.intValue(), _lng.intValue());
OverlayItem overlayItem = new OverlayItem(myPoint, atm_name, atm_address);
itemizedOverlay.addOverlay(overlayItem);
}
} catch(JSONException e){
e.printStackTrace();
}
mapOverlays.add(itemizedOverlay);

我想从 taskMK 获取数据,但数据总是返回 null ... 我该怎么做才能解决我的问题:( .. 对不起,我的英语不好。

最佳答案

与其在 taskMK 类中使用 getkq(),不如在执行 asyncTask 的 Activity 类中使用 setkq() .所以使用以下内容:

private class taskMK extends AsyncTask<Void,Void,JSONObject>{
String url;
JSONObject json;
MyActivityClassObject obj;

public taskMK(String tr, MyActivityClassObject obj){
this.url = tr;
this.obj = obj;
}
@Override
protected JSONObject doInBackground(Void... params) {
JSONParser jsonParser = new JSONParser();
JSONObject json = jsonParser.getJSONFromURL(url);
return json;
}

@Override
protected void onPostExecute(JSONObject result) {
this.obj.setkq(result);
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
}

在你的 Activity 类中你可以有这个功能:

public void setkq(JSONObject obj){
//Add your parsing logic here
}

关于android - 如何从异步任务返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11001215/

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