我有一个类扩展 asynctask
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;
}
}
活动中
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 ......我怎么办我解决了我的问题 :( .. 对不起,我用英语不好。