gpt4 book ai didi

java - Android 上的 Google Places API : No route to host

转载 作者:行者123 更新时间:2023-11-30 03:44:37 25 4
gpt4 key购买 nike

我在我的应用程序中使用 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)

为什么抛出这个异常?我怎样才能摆脱它?

最佳答案

java.net.SocketException: No route to host.

当没有路由连接到主机时会发生此异常。

我在这里发现了你的问题。

key = "&key=我的 key ";

替换为

String APIKEY="AIzaSyDcLMS0IKQL3N76rO-aD2thfO46r96OCQI";
key = "&key=" + APIKEY;

而且你链接错了。

您的链接:

https://maps.googleapis.com/maps/api/place/nearbysearch/output?jsonlocation=25.4509294,78.630043&radius=15000&types=food&key=key&sensor=false

链接必须是这样的:

String url = https://maps.googleapis.com/maps/api/place/search/json?location=25.4509294,78.630043&radius=15000&types=food&key=AIzaSyDcLMS0IKQL3N76rO-aD2thfO46r96OCQI&sensor=false

要获取 JSON,您可以使用我的代码。

String data = getUrlContents(url); //Calling method

方法的定义。

private String getUrlContents(String Url) 
{
StringBuilder content = new StringBuilder();
try {
URL url = new URL(Url);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()), 8);
String line;
while ((line = bufferedReader.readLine()) != null)
{
content.append(line + "\n");
}
bufferedReader.close();
}
catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}

关于java - Android 上的 Google Places API : No route to host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15185387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com