我无法让我的应用程序打开与在线JSON文件的URL的连接。我遵循指导如何获取inBackground线程来获取URL,但是当我在该URL上调用.connect()时,它似乎返回了inBackground函数,因为我测试了一个String,它将被修改并显示为Toast,并在httpConn.connect()之后立即修改String,根本没有任何变化。我确保我的权限在清单中是正确的,但也许我忽略了一些小的东西。
protected String doInBackground(URL... urls){
InputStream in = null;
String result = "test";
int responseCode = -1;
try {
URL url = urls[0];
URLConnection urlConn = url.openConnection();
if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException("URL is not an Http URL");
}
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.setRequestMethod("GET");
httpConn.setDoInput(true);
result = "get here";
httpConn.connect();
result = "don't get here";
responseCode = httpConn.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
in = httpConn.getInputStream();
}
httpConn.disconnect();
result = readIt(in, 10);
return result;
}
catch (Throwable t){
t.printStackTrace();
}
return result;
}
如果我只是忽视或不完全理解这一点,那么是什么导致那里没有任何联系或如何测试?如果需要,我可以附加代码。谢谢