错误凌空补丁 [英] Error in Volley Patch

查看:631
本文介绍了错误凌空补丁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

com.android.volley.NoConnectionError:java.net.ProtocolException:未知的方法'补丁';必须是[OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE]之一

  StringRequest putRequest =新StringRequest(Request.Method.PATCH,网址,
                 新Response.Listener<串GT;()
                 {
                     @覆盖
                     公共无效onResponse(字符串响应)
                     {                         Log.d(响应,响应);                     }
                 },
                 新Response.ErrorListener()
                 {
                      @覆盖
                      公共无效onErrorResponse(VolleyError错误)
                      {
                          Log.d(Error.Response,error.toString());                    }
                 }
             ){                 @覆盖
                 保护地图<字符串,字符串> getParams()方法
                 {
                         地图<字符串,字符串> PARAMS =新的HashMap<字符串,字符串> ();
                         params.put(名,我的名字);
                         params.put(时代,11);                         返回PARAMS;
                 }             };


解决方案

您确定您使用乱射库的版本是否正确?我只是想你code在棒棒堂,这是工作确定。
如果您正在使用凌空库作为外部项目,检查请求类的方法接口com.android.volley包。它应该有一个PATCH变量了。

 公共接口的方法{
        INT DE preCATED_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;
    }

如果不是,请使用最新版本的凌空库。

更新:

您是正确的,它显示在奇巧这个错误,但不是在棒棒糖。我想主要的问题是Java的HttpURLConnection类不支持补丁。
(我猜它在棒棒堂的作品,因为它是用Java 7和Java 7的HttpURLConnection类支持PATCH方法是什么?)
总之,您可以使用OkHttp图书馆解决此问题。该okhttp-URLConnection的模块实现了java.net.HttpURLConnection中的

以下JAR添加到您的libs文件夹:结果
<一href=\"https://search.maven.org/remote_content?g=com.squareup.okhttp&a=okhttp&v=LATEST\">okhttp-2.2.0.jar

<一href=\"https://search.maven.org/remote_content?g=com.squareup.okio&a=okio&v=LATEST\">okhttp-urlconnection-2.2.0.jar

<一href=\"http://central.maven.org/maven2/com/squareup/okhttp/okhttp-urlconnection/2.2.0/okhttp-urlconnection-2.2.0.jar\">okio-1.2.0.jar

创建一个OkHttpStack类:

 包com.example.temp;进口java.io.IOException异常;
进口java.net.HttpURLConnection中;
进口的java.net.URL;进口com.android.volley.toolbox.HurlStack;
进口com.squareup.okhttp.OkHttpClient;
进口com.squareup.okhttp.OkUrlFactory;公共类OkHttpStack扩展HurlStack {
    私人最终OkUrlFactory mFactory;    公共OkHttpStack(){
        这(新OkHttpClient());
    }    公共OkHttpStack(OkHttpClient客户端){
        如果(客户端== NULL){
            抛出新的NullPointerException(客户端不能为空);
        }
        mFactory =新OkUrlFactory(客户端);
    }    @覆盖保护HttpURLConnection类的createConnection(网址URL)抛出IOException
        返回mFactory.open(URL);
    }
}

使用下面的构造函数来创建一个凌空请求队列:

  Volley.newRequestQueue(getApplicationContext(),新OkHttpStack())加(putRequest)。

这是为我工作的奇巧吧。

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

             };

解决方案

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.

这篇关于错误凌空补丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆