0
我對Java/Android非常陌生,我有一個代碼來獲取網頁的響應。我有這樣的代碼,但由於某種原因,BufferedReader
的in.readLine()
總是返回null。爲什麼無法從網頁獲得迴應? Android
URL url = new URL(endPoint);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(connectionTimeout);
connection.setReadTimeout(readTimeout);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + Integer.toString(encodedParams.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Setup the POST payload
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(encodedParams);
wr.flush();
wr.close();
// Read the response
InputStream inputStream = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
不知道我犯了什麼錯誤。
在此先感謝。