0
try{
String a="http://10.0.2.2/test2.php";
Log.d("URL",a);
url = new URL(a);
Log.d("URL23",a);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
Log.d("URL3",a);
try {
System.setProperty("http.keepAlive", "false");
// just want to do an HTTP GET here
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Content-length", "0");
urlConnection.setUseCaches(false);
urlConnection.setAllowUserInteraction(false);
// give it 15 seconds to respond
urlConnection.setReadTimeout(35 * 1000);
urlConnection.connect();
int status = urlConnection.getResponseCode();
switch (status) {
case 200:Log.d("CASE","@@@@@@@@@@@@@@@@ 200");
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
Log.d("CASE 201 String",sb.toString());
}
}
}
它是在getResponseCode
拋出異常,並且還在urlConnection.getInputStream()
(如果不使用開關的情況)。任何人都可以請告訴我爲什麼我無法建立連接使用HttpURLConnection
和我的代碼錯誤。HttpURLConnection類(未建立的連接)
您是否在清單中指定了Internet權限? –
是的,我已經使用模擬器或從設備添加權限 – user2796222
,因爲您正在訪問本地url? –