Error in Volley Patch
Asked Answered
B

2

7

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; 
                 }

             };
Benzoin answered 4/2, 2015 at 5:33 Comment(0)
T
22

Are you sure you are using correct version of Volley Library? I just tried your code in Lollipop and it is working OK. If you are using Volley library as external project, check the Method interface of Request class in com.android.volley package. It should have a PATCH variable in it.

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 Volley library.

UPDATE:

You are correct, it is showing this error in Kitkat, but not in Lollipop. I guess the main problem is that HTTPUrlConnection of Java does not support PATCH. (I guess it works in Lollipop because it is using Java 7 and HTTPUrlConnection of Java 7 supports PATCH method?) Anyhow, You can use the OkHttp Library to correct this problem. The okhttp-urlconnection module implements the java.net.HttpURLConnection

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

Create a 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 Volley RequestQueue:

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

It is working for me on Kitkat now.

Trichomoniasis answered 4/2, 2015 at 6:11 Comment(4)
used latest version and PATCH declared inside Request classBenzoin
You should accept the answer if you think it is correct.Trichomoniasis
@GobletSky Hello, OkUrlFactory is now deprecated, have you an idea to solve the problem without it ?Celinecelinka
I had the same problem (I was trying to make a Volley JSONObjectRequest with PATCH method on KitKat). I think these tips might help someone too: 1- If you have issues with OkHTTP, try downloading okhttp-2.3.0.jar (newer versions did not work for me). 2- Be careful if you use a unique request queue in you app (MyApplication.java, maybe?) as this newRequestQueue with OkHttpStack may break other requests types like GET. I ended up defining two queues: this one only for (PATCH&pre-5.0) and my usual requestQueue for any other. 3- This may work for JSONArrayRequest too.Secundines
R
-1

While sending request use POST. In headers just override http method to PATCH. For me now its working in volley even in kitkat version.

header.put("X-HTTP-Method-Override", "PATCH");
Ripuarian answered 21/11, 2016 at 10:14 Comment(1)
your server should allow Method override to let this workParham

© 2022 - 2024 — McMap. All rights reserved.