【发布时间】:2018-03-18 05:20:08
【问题描述】:
这是我在谷歌地图中遇到此错误的 java 代码
public String getJSONFromUrl(String url)
{
try {
Log.e("jsonURL", url);
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
json = sb.toString();
is.close();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return json;
}
我这里调用这个方法
protected String doInBackground(Void... params) {
JSONParser jParser = new JSONParser();
String json = jParser.getJSONFromUrl(url);
return json;
}
请建议我解决此问题的方法。
这里是日志
10-06 18:54:18.529 17583-18349/com.app.io.taxiapp W/System.err:原因:android.system.ErrnoException:连接失败:ECONNREFUSED(连接被拒绝) 10-06 18:54:18.529 17583-18349/com.app.io.taxiapp W/System.err:在 libcore.io.Posix.connect(本机方法) 10-06 18:54:18.529 17583-18349/com.app.io.taxiapp W/System.err: 在 libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111) 10-06 18:54:18.529 17583-18349/com.app.io.taxiapp W/System.err: 在 libcore.io.IoBridge.connectErrno(IoBridge.java:137) 10-06 18:54:18.529 17583-18349/com.app.io.taxiapp W/System.err: 在 libcore.io.IoBridge.connect(IoBridge.java:122) 10-06 18:54:18.529 17583-18349/com.app.io.taxiapp W/System.err: ... 21 更多 10-06 18:54:18.539 17583-17708/com.app.io.taxiapp D/libEGL: eglInitialize EGLDisplay = 0xeeafa60c 10-06 18:54:18.539 17583-17708/com.app.io.taxiapp D/libEGL: eglTerminate EGLDisplay = 0xeeafa66c 10-06 18:54:18.549 17583-17583/com.app.io.taxiapp I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@7f3946f time:11454827 10-06 18:54:18.559 17583-18349/com.app.io.taxiapp E/Buffer 错误:转换结果时出错 java.io.IOException:尝试在关闭的流上读取。
【问题讨论】:
-
你能把这个:
Log.e("Buffer Error", "Error converting result " + e.toString());替换成Log.e("Buffer Error", "Error converting result ", e);并从 logcat 发布整个堆栈跟踪吗? -
请检查我已编辑问题。
-
Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)我认为这是您问题的根源。您的连接被拒绝,因此您的InputStreamis为空/null,因为is = httpEntity.getContent();失败。 -
相同的代码在其他设备上运行良好,但在我的设备 android M 中应用程序崩溃了。
标签: java android google-maps