尝试使用HttpURLConnection
与帖子数据连接http://ws.audioscrobbler.com/2.0/时,我得到(随机)
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();
}
答案 0 :(得分:1)
我检查了conn.getResponseCode()
并获得了403.异常可能是由于此原因。
答案 1 :(得分:0)
好吧试试这可能有帮助
public void httppost()
{
URL="Your URL";
InputStream is = null;
//Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
namevaluepair.add(new BasicNameValuePair("name1",value1));
namevaluepair.add(new BasicNameValuePair("name2",value2));
String enc_url = urlEncode(URL);
HttpPost httpPost = new HttpPost(enc_url );
httpPost.setEntity(new UrlEncodedFormEntity(namevaluepair));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
String response = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
}
答案 2 :(得分:0)
您需要指定内容长度。
if(postData != null){
byte[] requestContent = getQuery(postData);
conn.setDoOutput(true);
// Specify content type
conn.setRequestProperty("Content-Type", mRequestContentType);
conn.setRequestProperty("Content-Length",
Integer.toString(requestContent.length));
conn.setFixedLengthStreamingMode(requestContent.length);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8"));
writer.write(requestContent);
writer.close();
os.close();
}
答案 3 :(得分:0)
对于4.4之前的Android版本,我认为您需要添加:
conn.setRequestProperty("connection", "close");
EOFException是最初在此处报告的已知错误: https://github.com/google/google-http-java-client/issues/213