gpt4 book ai didi

android - HttpURLConnection getInputStream() 中的 EOFException 和 FileNotFoundException

转载 作者:行者123 更新时间:2023-11-30 03:27:42 25 4
gpt4 key购买 nike

尝试连接到 http://ws.audioscrobbler.com/2.0/ 时使用 HttpURLConnection 发布数据,我得到(随机)

EOFException

FileNotFoundException: http://ws.audioscrobbler.com/2.0/

在运行 Android 4.2.2 的 Nexus 4 上。

有人可以帮忙吗?

编辑

我升级到 4.3:同样的问题。

public InputStream getData(String url_str, List<NameValuePair> postData) {

Map<String, Object> response = new HashMap<String, Object>();

InputStream is = null;
HttpURLConnection conn = null;

try {

URL url = new URL(url_str);

conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(CONNECTION_TIMEOUT);
conn.setConnectTimeout(READ_TIMEOUT);
conn.setRequestMethod("POST");
conn.setDoInput(true);

if(postData != null){

conn.setDoOutput(true);

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(postData));
writer.close();
os.close();
}


// Starts the query

conn.connect();


// Get and return response

is = conn.getInputStream();

return is;

} catch (IOException e) {

// ...

} finally {

if(conn != null){
conn.disconnect();
}

if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;

for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");

result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}

return result.toString();
}

最佳答案

我检查了 conn.getResponseCode() 并得到了一个 403。异常可能是由于那个。

关于android - HttpURLConnection getInputStream() 中的 EOFException 和 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17967731/

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