Mistake in volleyball surge

com.android.volley.NoConnectionError: java.net.ProtocolException: Unknown method 'PATCH'; must be one of [OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE]

StringRequest putRequest = new StringRequest(Request.Method.PATCH, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d("Response", response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("Error.Response", error.toString()); } } ) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String> (); params.put("name", "My Name"); params.put("age", "11"); return params; } }; 
+7
java android android-volley
source share
2 answers

Are you sure you are using the correct version of Volley Library? I just tried my code in Lollipop and it works fine. If you use the Volley library as an external project, check the interface of the Request class method in the com.android.volley package. It must have a PATCH variable.

 public interface Method { int DEPRECATED_GET_OR_POST = -1; int GET = 0; int POST = 1; int PUT = 2; int DELETE = 3; int HEAD = 4; int OPTIONS = 5; int TRACE = 6; int PATCH = 7; } 

If not, use the latest version of the Volley library.

UPDATE:

You are right, it shows this error in Kitkat, but not in Lollipop. I assume that the main problem is that HTTPUrlConnection Java does not support PATCH. (I think this works in Lollipop because it uses Java 7, and HTTPUrlConnection Java 7 supports the PATCH method?) Anyway, you can use the OkHttp Library to fix this problem. The okhttp-urlconnection module implements java.net.HttpURLConnection

Add the following folder to the libs folder:
okhttp-2.2.0.jar
okhttp-urlconnection-2.2.0.jar
okio-1.2.0.jar

Create the OkHttpStack class:

 package com.example.temp; 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); } } 

Use the following constructor to create a volleyball request:

 Volley.newRequestQueue(getApplicationContext(),new OkHttpStack()).add(putRequest); 

Now he works in Kitkat.

+20
source share

When sending a request, use POST. The headers simply override the http PATCH method. For me, now his work in salvo, even in the kitkat version.

 header.put("X-HTTP-Method-Override", "PATCH"); 
-one
source share

All Articles