我正在尝试使用排球做补丁请求。但它引发了一个例外:
com.android.volley.VolleyError: java.lang.IllegalArgumentException: method PATCH must have a request body.
任何帮助表示赞赏!
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
public class CustomJsonObjectRequest extends Request<JSONObject> {
private Listener<JSONObject> listener;
private Map<String, String> params;
public CustomJsonObjectRequest(int method, String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(Method.PATCH, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
@Override
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
return params;
};
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return Response.error(new ParseError(e));
} catch (JSONException je) {
je.printStackTrace();
return Response.error(new ParseError(je));
}
}
@Override
public byte[] getBody() throws AuthFailureError {
Map<String, String> params = getParams();
if (params != null && params.size() > 0) {
return encodeParameters(params, getParamsEncoding());
}
return null;
}
private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
StringBuilder encodedParams = new StringBuilder();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
encodedParams.append('&');
}
return encodedParams.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
// TODO Auto-generated method stub
Map<String, String> headers = new HashMap<String, String>();
headers.put("API_TOKEN", MainActivity.getAPIToken());
headers.put("Content-Type", "application/json");
return headers;
}
@Override
protected void deliverResponse(JSONObject response) {
// TODO Auto-generated method stub
Log.d("check deliverResponse:", ""+response);
}
}
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import com.android.volley.toolbox.HurlStack;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
public class OkHttpStack extends HurlStack {
private final OkUrlFactory mFactory;
public OkHttpStack() {
this(new OkHttpClient());
}
public OkHttpStack(OkHttpClient client) {
if (client == null) {
throw new NullPointerException("Client must not be null.");
}
mFactory = new OkUrlFactory(client);
}
@Override protected HttpURLConnection createConnection(URL url) throws IOException {
return mFactory.open(url);
}
}
Map<String, String> params = new HashMap<String, String>();
params.put("new_id","7986");
String id = "7984";
CustomJsonObjectRequest customReq = new CustomJsonObjectRequest(
Request.Method.PATCH, MainURL.APIURL + id,params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jObjGags) {
Log.d("check json:", "" + jObjGags);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("check error:", "" + error);
}
});
Volley.newRequestQueue(getActivity(),new OkHttpStack()).add(customReq);
我发送请求人作为Map params。它抛出一个异常,说明PATCH请求必须有一个请求体。
答案 0 :(得分:2)
最后得到了解决我问题的一些解决方法!!
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import com.android.volley.toolbox.HttpClientStack;
public class PatchTask extends AsyncTask<String, Void, JSONObject>{
JSONObject jobj;
Activity activity;
public PatchTask(Activity activity) {
this.activity = activity;
}
@Override
protected JSONObject doInBackground(String... params) {
// TODO Auto-generated method stub
HttpClient httpclient = new DefaultHttpClient();
HttpClientStack clientStack = new HttpClientStack(httpclient);
BufferedReader reader;
String line=null;
try {
HttpResponse response =clientStack.performRequest(customReq, headers);
Log.d("patch response>>", ""+response.toString());
HttpEntity entity = response.getEntity();
Log.d("patch entity", ""+entity.toString());
if (entity != null) {
InputStream instream = entity.getContent();
Log.d("patch instream>>", ""+instream.toString());
reader = new BufferedReader(new InputStreamReader(
instream, "UTF-8"), 8192);
line = reader.readLine().toString();
instream.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (line != null) {
jobj = new JSONObject(line);
}
} catch (JSONException e1) {
e1.printStackTrace();
}
return jobj;
}
@Override
protected void onPostExecute(JSONObject result) {
Log.d("patch post result:", ""+result);
}
}