gpt4 book ai didi

java - Android HttpURLConnection 奇怪的响应

转载 作者:可可西里 更新时间:2023-11-01 16:47:05 26 4
gpt4 key购买 nike

我正在尝试获取 Http 请求的响应数据。我的代码如下所示:

public class Networking {

// private variables
private URL mUrl;
private InputStream mInputStream;

public void Networking() {}

public InputStream setupConnection(String urlString) {

// public variables
int connectionTimeout = 10000; // milliseconds
int readTimeout = 15000; // milliseconds

try {
mUrl = new URL(urlString);

try {
// initialize connection
HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();

// setup connection
connection.setConnectTimeout(connectionTimeout);
connection.setReadTimeout(readTimeout);
connection.setRequestMethod("GET");
connection.setDoInput(true);

// start the query
try {
connection.connect();
int response = connection.getResponseCode();

if (response == 200) {
// OK
mInputStream = connection.getInputStream();
return mInputStream;

} else if (response == 401) {
// Unauthorized
Log.e("Networking.setupConn...", "unauthorized HttpURL connection");
} else {
// no response code
Log.e("Networking.setupConn...", "could not discern response code");
}
} catch (java.io.IOException e) {
Log.e("Networking.setupConn...", "error connecting");
}


} catch (java.io.IOException e) {
Log.e("Networking.setupConn...", "unable to open HTTP Connection");
}
} catch (java.net.MalformedURLException e) {
Log.e("Networking.setupConn..", "malformed url " + urlString);
}

// if could not get InputStream
return null;
}

public String getStringFromInputStream() {
BufferedReader br = null;
StringBuilder sb = new StringBuilder(5000);
String line;

try {
br = new BufferedReader(new InputStreamReader(mInputStream), 512);
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (java.io.IOException e) {
Log.e("BufferReader(new ..)", e.toString());
return null;
} finally {
if(br != null) {
try {
br.close();
}catch (java.io.IOException e) {
Log.e("br.close", e.toString());
}
}
}
return sb.toString();
}
}

问题是 getStringFromInputStream 函数总是返回一个 4063 字节长的字符串。总是!不管 url 是什么。

我查了一下,(line = br.readLine())部分代码总是返回一个固定长度为4063的字符串。

我不明白这个。请帮忙。

最佳答案

这是适合我的代码:

public String getDataFromUrl(String httpUrlString)
URL url = new URL(httpUrlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
responseCode = urlConnection.getResponseCode();
if (responseCode != HttpStatus.SC_OK) {
return null;
} else { // success
BufferedReader in = null;
StringBuffer str = new StringBuffer();
try {
in = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
str.append(inputLine);
}
} finally {
if (null != in) {
in.close();
}
urlConnection.disconnect();
}
return str.toString();
}
}

关于java - Android HttpURLConnection 奇怪的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39079281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com