2

我对 Java/Android 很陌生,我有一个代码来获取网页的响应。我有这样的代码,但由于某种原因,BufferedReader'sin.readLine()总是返回空值。

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"));

不知道我在哪里犯了错误。

提前致谢。

4

1 回答 1

0

尝试

connection.connect();

在阅读回复之前

于 2013-01-22T12:01:17.620 回答