嗨朋友我正在开发一个需要的应用程序:
我的网络服务是:
https://maps.googleapis.com/maps/api/geocode/json
因为我在转换countryname From Langitude和Latitude时遇到以下错误。
错误是:
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();
}
请帮帮我。告诉我我在哪里做错了。
提前多多谢谢。
答案 0 :(得分:3)
启用严格模式以删除此异常。只需将此代码放在您的活动的super.onCreate()之后,如下所示: -
super.onCreate(savedInstanceState);
**StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);**
setContentView(R.layout.main);