【发布时间】:2014-04-14 08:47:45
【问题描述】:
嗨,朋友们,我正在开发一个应用程序,它需要:
- 将所有国家/地区名称放入列表中。
- 国家/地区列表来自 JSON。
我的网络服务是:
https://maps.googleapis.com/maps/api/geocode/json
在从经度和纬度转换国家名称时出现以下错误。
错误是:
Maps Call URL::android.os.NetworkOnMainThreadException
TMP ALBUM CountryAll::Anguilla
Error in Parsig JSON:org.json.JSONException: Index 4 out of range [0..1)
Error in Post Execute:java.lang.NullPointerException
我的 JSON 代码是:
public class GetLatLngAsyncTask extends AsyncTask<ArrayList<String>, Void, String>
{
JSONObject jObject;
JSONObject places = null;
String lat;
ProgressDialog progressDialog;
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = new ProgressDialog(Map_View.this);
progressDialog.setTitle("Getting Users Location");
progressDialog.setMessage("Please wait");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
@Override
protected String doInBackground(ArrayList<String>... params)
{
try
{
double lng1 = 0;
double lat1 = 0;
lng1=lat1=0;
StringBuilder stringBuilder = new StringBuilder();
JSONObject jsonObject = new JSONObject();
try
{
for(int i=0;i<MapListData.size();i++)
{
String countryAll=MapListData.get(i).get("country");
countryAll = countryAll.replaceAll(" ","%20");
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + countryAll + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1)
{
stringBuilder.append((char) b);
}
try
{
jsonObject = new JSONObject(stringBuilder.toString());
Log.v("TMP ALBUM","TMP ALBUM CountryAll::"+countryAll);
lng1 = ((JSONArray)jsonObject.get("results")).getJSONObject(i)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
lat1 = ((JSONArray)jsonObject.get("results")).getJSONObject(i)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
Log.v("Langitude is:",""+lng1);
Log.v("Latitude is:",""+lat1);
tmp_album1 = new HashMap<String, String>();
tmp_album1.put("Lng",""+lng1);
tmp_album1.put("lat",""+lat1);
MapListData1.add(tmp_album1);
Log.v("MAP LIST","MAP LIST1::"+MapListData1);
}
catch (JSONException e)
{
Log.v("Error","Error in Parsig JSON:"+e);
}
}
}
catch (Exception e)
{
Log.v("Error In MAPS","Maps Call URL::"+e);
}
}
catch(Exception e)
{
Log.v("Error In MAPS","Maps ASYC::"+e);
}
return lat;
}
@Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
try
{
for(int i=0;i<MapListData1.size();i++)
{
double Lang=Double.valueOf(MapListData1.get(i).get("lng"));
double Lat=Double.valueOf(MapListData1.get(i).get("lat"));
MarkerOptions marker = new MarkerOptions()
.position(new LatLng(Lang,Lat))
.title("Your Friend Is in "+country);
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
googleMap.addMarker(marker);
Log.v("Marker","marker Added");
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(Lang,Lat))
.zoom(2).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
catch(Exception e)
{
Log.v("ERROR","Error in Post Execute:"+e);
}
progressDialog.dismiss();
}
请帮助我。告诉我哪里出错了。
非常感谢。
【问题讨论】:
-
你为什么在后台使用
mHandler.post(new Runnable())?删除mHanlder -
删除你的
Handler代码部分,因为网络操作不能在主线程中运行。在AsyncTask中使用Handler是愚蠢的。 -
你不能在后台使用
UI,删除Handler后你需要删除googleMap.addMarker(marker);和所有涉及UI的代码 -
从
background返回一个标记列表到onPostExecute,然后在该方法中添加所有标记 -
记录您的异常堆栈跟踪并在此处发布。以及您如何首先调用您的 asynctask,希望不是直接调用
doInBackground()...
标签: android json web-services networkonmainthread