在我的Android应用程序中,我很难从给定的URL接收JSON列表响应。我不确定我是否错过了启动GET调用的一步,或者问题在web服务方面。当代码到达"getInputStream“行时,它就会崩溃。
if (url != null) {
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
catch (IOException e) {
e.printStackTrace();
}
}给出的错误如下所示与NetworkOnMainThread异常以及其他一些异常有关。注意事项:这是在"onCreate“方法中调用的方法,它也可能是问题的根源。
发布于 2015-07-04 04:07:25
好吧,这是最后一期,谢谢你的澄清,丹尼尔。我变懒了,没有把它放进ASyncTask里。现在工作得很好,谢谢!
https://stackoverflow.com/questions/31216579
复制相似问题