gpt4 book ai didi

java - Android 平台上的 HttpsURLConnectionImpl.getInputStream() 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:27:37 26 4
gpt4 key购买 nike

这是我的任务。给定一对纬度和经度,我想从 Google Places API 获取一个 JSON。以下函数 reverseGeoCoding 返回字符串 JSON。

public String reverseGeoCoding(String lat, String lng) {
latitude = lat;
longitude = lng;
String json = new String();
String url = new String();

URL googleAPI;
try {
url = URL + "location=" + latitude + "," + longitude + "&" + "radius=" + radius + "&" + "language=" + language + "&" + "sensor=false" + "&" + "key=" + APIKey;

googleAPI = new URL(url);
Log.d("TEST", googleAPI.toString());
URLConnection gc = (URLConnection) googleAPI.openConnection();
Log.d("TEST", gc.toString());

InputStream is = gc.getInputStream();
Log.d("TEST", "InputStream created");
BufferedReader in = new BufferedReader(new InputStreamReader(is));
Log.d("TEST", "reader created");
String inputLine;
while ((inputLine = in.readLine()) != null) {
json = json + inputLine + "\n";
Log.d("TEST", inputLine);
}

in.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return json;
}

但是,getInputStream() 不起作用。我已经在网络浏览器上检查了 url 请求,它工作正常(返回一个 JSON 文档)。

错误信息如下。 (为什么第 17 和 18 行会产生错误)

1. 12-11 00:46:02.862: W/System.err(12953): android.os.NetworkOnMainThreadException
2. 12-11 00:46:02.872: W/System.err(12953): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
3. 12-11 00:46:02.872: W/System.err(12953): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
4. 12-11 00:46:02.872: W/System.err(12953): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
5. 12-11 00:46:02.872: W/System.err(12953): at java.net.InetAddress.getAllByName(InetAddress.java:220)
6. 12-11 00:46:02.872: W/System.err(12953): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
7. 12-11 00:46:02.872: W/System.err(12953): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
8. 12-11 00:46:02.882: W/System.err(12953): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
9. 12-11 00:46:02.882: W/System.err(12953): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10. 12-11 00:46:02.882: W/System.err(12953): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11. 12-11 00:46:02.882: W/System.err(12953): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
12. 12-11 00:46:02.882: W/System.err(12953): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
13. 12-11 00:46:02.882: W/System.err(12953): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
14. 12-11 00:46:02.882: W/System.err(12953): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
15. 12-11 00:46:02.882: W/System.err(12953): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
16. 12-11 00:46:02.882: W/System.err(12953): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
17. 12-11 00:46:02.882: W/System.err(12953): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
18. 12-11 00:46:02.894: W/System.err(12953): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
19. 12-11 00:46:02.894: W/System.err(12953): at locationpredict.POIList.reverseGeoCoding(POIList.java:55)
20. 12-11 00:46:02.894: W/System.err(12953): at locationpredict.TestMain$1.onClick(TestMain.java:62)

最佳答案

由于人们在主线程上使用繁重的网络操作导致应用程序无响应,因此添加了这个新功能以防止人们这样做。
您应该使用 AsyncTask 或服务来执行此操作。

        private class GetJsonTask extends AsyncTask<String, Void, String>{


@Override
protected String doInBackground(String... params) {
try {
latitude = params[0];
longitude = params[1];
String json = new String();
String url = new String();
URL googleAPI;


url = URL + "location=" + latitude + "," + longitude + "&" + "radius=" + radius + "&" + "language=" + language + "&" + "sensor=false" + "&" + "key=" + APIKey;

googleAPI = new URL(url);
Log.d("TEST", googleAPI.toString());
URLConnection gc = (URLConnection) googleAPI.openConnection();
Log.d("TEST", gc.toString());

InputStream is = gc.getInputStream();
Log.d("TEST", "InputStream created");
BufferedReader in = new BufferedReader(new InputStreamReader(is));
Log.d("TEST", "reader created");
String inputLine;
while ((inputLine = in.readLine()) != null) {
json = json + inputLine + "\n";
Log.d("TEST", inputLine);
}

in.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}

@Override
protected void onPostExecute(String result) {
parseJson(result); // json should be stored in the result
}
}

private void parseJson(String json){
JSONObject obj = new JSONObject(json);
....

}

这样调用它:

String[] data = new String[2];
data[0] = lat;
data[1] = long;
new GetJsonTask().execute(data);

或者你也可以通过在你的onCreate()方法中添加以下代码在调用该方法之前绕过它,但我不推荐这种方法。仅用于测试。

   if( Build.VERSION.SDK_INT >= 9){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

关于java - Android 平台上的 HttpsURLConnectionImpl.getInputStream() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13812051/

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