我在我的应用程序中使用 google places api 来显示附近的食物位置。
我正在使用以下代码:
public class Background extends AsyncTask<String, Integer, String> {
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
@Override
protected String doInBackground(String... params) {
pfr = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
lctn = "location=" + latitude + "," + longitude;
key = "&key=my key";
type = "&types=" + pfr.getString("type", "food");
radius = "&radius=" + pfr.getString("radius", "500");
sensor = "&sensor=false";
StringBuilder requesturl = new StringBuilder(googlegives);
requesturl.append(lctn);
requesturl.append(radius);
requesturl.append(type);
requesturl.append(key);
requesturl.append(sensor);
DefaultHttpClient client = new DefaultHttpClient();
HttpGet req = new HttpGet(requesturl.toString());
Log.d("create", "0");
try {
HttpResponse res = client.execute(req);
HttpEntity jsonentity = res.getEntity();
String data = EntityUtils.toString(jsonentity);
JSONObject jsonobj = new JSONObject(data);
JSONArray resarray = jsonobj.getJSONArray("results");
if (resarray.length() == 0) {
Toast.makeText(getApplicationContext(), "nothing found",
Toast.LENGTH_LONG).show();
} else {
int len = resarray.length();
for (int j = 0; j < len; j++) {
lon = resarray.getJSONObject(j).getJSONObject("geometry")
.getJSONObject("location").getDouble("lng");
lat = resarray.getJSONObject(j).getJSONObject("geometry")
.getJSONObject("location").getDouble("lat");
LatLng latLng = new LatLng(lat, lon);
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title(
resarray.getJSONObject(j).getJSONObject("name")
.toString())
.snippet(
resarray.getJSONObject(j)
.getJSONObject("formatted_address")
.toString()));
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
获取纬度和经度作为获取用户当前位置的全局变量,在我的手机上运行此应用程序时出现在 log cat 中
03-03 16:52:37.859: W/System.err(4780): java.net.SocketException: No route to host
03-03 16:52:37.906: W/System.err(4780): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
03-03 16:52:37.906: W/System.err(4780): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
03-03 16:52:37.906: W/System.err(4780): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
03-03 16:52:37.914: W/System.err(4780): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
03-03 16:52:37.914: W/System.err(4780): at java.net.Socket.connect(Socket.java:1002)
03-03 16:52:37.914: W/System.err(4780): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
03-03 16:52:37.914: W/System.err(4780): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
03-03 16:52:37.914: W/System.err(4780): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-03 16:52:37.914: W/System.err(4780): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-03 16:52:37.914: W/System.err(4780): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
03-03 16:52:37.914: W/System.err(4780): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-03 16:52:37.914: W/System.err(4780): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-03 16:52:37.921: W/System.err(4780): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-03 16:52:37.921: W/System.err(4780): at com.abhishekbietcs.locomap.Mapme$Background.doInBackground(Mapme.java:258)
03-03 16:52:37.921: W/System.err(4780): at com.abhishekbietcs.locomap.Mapme$Background.doInBackground(Mapme.java:1)
03-03 16:52:37.921: W/System.err(4780): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-03 16:52:37.921: W/System.err(4780): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
03-03 16:52:37.921: W/System.err(4780): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
03-03 16:52:37.921: W/System.err(4780): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
03-03 16:52:37.921: W/System.err(4780): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
03-03 16:52:37.921: W/System.err(4780): at java.lang.Thread.run(Thread.java:1019)
为什么会抛出这个异常?我怎样才能摆脱它?