返回數據,我有一個類繼承的AsyncTask如何從的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獲取數據,但數據總是返回空...怎麼辦我解決我的問題:(..對不起,我用英語並不好。
我有更新我的問題,你能幫助我,謝謝:「> – HoangQBH