我無法讓我的應用程序打開到在線JSON文件的URL的連接。我遵循指導如何獲得inBackground線程來獲取URL,但是當我在該URL上調用.connect()時,它似乎返回inBackground函數,因爲我測試的只是一個將被修改並顯示爲Toast,並在httpConn.connect()之後立即修改字符串根本沒有任何改變。我確信我的權限在清單中是正確的,但也許有一些我忽略的東西。HTTP URL連接Android
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;
}
是什麼原因造成有不被任何連接或如何測試,如果我只是遠眺或沒有充分理解這一點任何想法?如果需要,我可以額外添加代碼謝謝
請發佈'AsyncTask'類代碼。 – Hasanaga