我正在尝试使用来自 android Volley 框架的自定义版本的 Request 类。我设法处理 GET 自定义请求,但 POST 失败。我正在覆盖 getBody() 方法并返回 byte[]。但 Volley 抱怨连接已经打开。
代码:
@Override
public byte[] getBody() throws AuthFailureError {
JSONObject json = new JSONObject();
try {
json.put("date_in_millis", this.newsDateInMillis);
json.put("title", URLEncoder.encode(this.newsTitle.replace("\n", "").replace("\r", ""), "UTF-8"));
return new StringEntity("data=" + json.toString(), "UTF-8").toString().getBytes();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
例外:
05-24 21:51:08.076: E/Volley(5535): [177] NetworkDispatcher.run: Unhandled exception
java.lang.IllegalStateException: Already connected
05-24 21:51:08.076: E/Volley(5535): java.lang.IllegalStateException: Already connected
05-24 21:51:08.076: E/Volley(5535): at java.net.URLConnection.checkNotConnected(URLConnection.java:464)
05-24 21:51:08.076: E/Volley(5535): at java.net.URLConnection.setDoOutput(URLConnection.java:878)
05-24 21:51:08.076: E/Volley(5535): at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:225)
05-24 21:51:08.076: E/Volley(5535): at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
05-24 21:51:08.076: E/Volley(5535): at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
05-24 21:51:08.076: E/Volley(5535): at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
05-24 21:51:08.076: E/Volley(5535): at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
有人以这种方式使用过Volley Request吗?
谢谢