安卓:Volley HTTP Patch 请求

5
我刚开始从我的现有网络库移植到Android的Volley。到目前为止,我已经成功地实现了Volleys ImageLoader(适用时)。现在,我正在尝试启动并运行我的第一个http调用,但是发现了这个错误。
注意:我故意从PATCH请求开始,因为我将经常使用它们。此外,我的Volley版本确实支持patch: https://android.googlesource.com/platform/frameworks/volley/+/master/src/com/android/volley/Request.java https://android.googlesource.com/platform/frameworks/volley/+/master/src/com/android/volley/toolbox/HurlStack.java 堆栈跟踪:
E/InputDialogFragment(27940): VolleyError: java.net.ProtocolException: Connection already established
D/Volley  (27940): [1] MarkerLog.finish: (67   ms) [ ] https://mobile.example.com/m/api/v1/user/ 0xb33a3c8d NORMAL 2
D/Volley  (27940): [1] MarkerLog.finish: (+0   ) [ 1] add-to-queue
D/Volley  (27940): [1] MarkerLog.finish: (+0   ) [544] cache-queue-take
D/Volley  (27940): [1] MarkerLog.finish: (+0   ) [544] cache-miss
D/Volley  (27940): [1] MarkerLog.finish: (+0   ) [545] network-queue-take
D/Volley  (27940): [1] MarkerLog.finish: (+14  ) [545] post-error
D/Volley  (27940): [1] MarkerLog.finish: (+53  ) [ 1] done

PATCH请求

    HashMap<String, Object> values = new HashMap<String, Object>();
    values.put(mParam, val);
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.PATCH, APIConstants.URL_USER, new JSONObject(values),
        new Response.Listener<JSONObject>(){
            @Override
            public void onResponse(JSONObject response){
               // Blah do stuff here 
                mProgressDialog.dismiss();
            }
        },
        new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError error){
                Log.e(TAG, "VolleyError: " + error.getMessage());
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                VolleySingleton.getInstance().addCookie(headers);
                return headers;
            }
    };
    VolleySingleton.getInstance().getRequestQueue().add(request);

是的,我计划最终为StringRequest、JsonObjectRequest等构建类...但目前我只想让一个运行起来。
此外,如果你想知道addCookie的情况,目前我更喜欢将我的cookie保存在偏好设置中,因为我对CookieManager不太熟悉。
VolleySingleton
public class VolleySingleton {
    private static final String COOKIE_KEY = "Cookie";
    private static VolleySingleton mInstance = null;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private SharedPreferences mPreferences;
    private VolleySingleton(){
        mRequestQueue = Volley.newRequestQueue(MyApplication.getAppContext());
        mImageLoader = new ImageLoader(this.mRequestQueue, new ImageLoader.ImageCache() {
            private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
            public void putBitmap(String url, Bitmap bitmap) {
                mCache.put(url, bitmap);
            }
            public Bitmap getBitmap(String url) {
                return mCache.get(url);
            }
        });
        mPreferences = MyApplication.getAppContext().getSharedPreferences(PrefConstants.PREFERENCES, 0);
    }

    public static VolleySingleton getInstance(){
        if(mInstance == null){
            mInstance = new VolleySingleton();
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue(){
        return this.mRequestQueue;
    }

    public ImageLoader getImageLoader(){
        return this.mImageLoader;
    }

    public final void addCookie(Map<String, String> headers) {
        String cookie = mPreferences.getString(PrefConstants.PREF_COOKIE, null);
        if(cookie != null){
            headers.put(COOKIE_KEY, cookie);
        }
    }
}

注意到GET和POST按预期工作,所以这可能只是一个补丁问题。 - Brice
3个回答

3
问题:

Volleys HurlStack (HttpUrlConnection) 已经支持了PATCH。然而,每当您尝试发出PATCH请求时,它仍会抛出我标题和堆栈跟踪中发布的异常。

解决方法:

1)强制Volley使用HttpClientStack。

下面是我VolleySingleton构造函数的更新版本。这“有效”,但显然浪费了Hurl实现,如果(Build.VERSION.SDK_INT >= 9)则被认为更好。更不用说Google计划在未来放弃apache HttpClient。

private VolleySingleton(){
    mPreferences = MyApplication.getAppContext().getSharedPreferences(PrefConstants.PREFERENCES, 0);

    String userAgent = "volley/0";
    try {
        String packageName = MyApplication.getAppContext().getPackageName();
        PackageInfo info = MyApplication.getAppContext().getPackageManager().getPackageInfo(packageName, 0);
        userAgent = packageName + "/" + info.versionCode;
    } catch (NameNotFoundException e) {}

    HttpStack httpStack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
    mRequestQueue = Volley.newRequestQueue(MyApplication.getAppContext(), httpStack);
    mImageLoader = new ImageLoader(this.mRequestQueue, new ImageLoader.ImageCache() {
        private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
        public void putBitmap(String url, Bitmap bitmap) {
            mCache.put(url, bitmap);
        }
        public Bitmap getBitmap(String url) {
            return mCache.get(url);
        }
    });
}

2) 继续使用上述VolleySingleton仅限于补丁; 将其重命名为VolleySingletonPatch(),然后当然为所有其他非补丁调用创建一个默认的VolleySingleton()。 (比1更好,但仍不是最佳选择)

3) 解决在HurlStack中抛出异常的问题,尽管Volley已经实现了PATCH。这将是最佳选择,但我更喜欢避免直接操作Volley或不必要地扩展自己的HttpStacks。

我没有回答这个问题,因为我非常感谢任何见解,当然也欢迎比我提出的更好的选择。


2

我也遇到了这个问题,我的解决方案是放弃使用内置的Apache HTTP组件,而是使用Square的OkHttp库,使用这个HttpStack实现作为Volley的传输方式。非常好用。


你好@akent,你介意分享一下如何在你的类中实际使用这个实现吗?我的意思是我按照链接所说的做了,并添加了OkHttp库,还在AppController.getInstance().addToRequestQueue(sendData);之前添加了Volley.newRequestQueue(getApplicationContext(), new OkHttpStack());。这样正确吗?谢谢! - ito

1

我知道这个问题很旧了,但提供解决方案可以帮助那些尚未遇到同样挑战的人。

JSONObject param = new JSONObject();
    try {
        param.put(KEY 1, VALUE 1);
        param.put(KEY 2, VALUE 2);

    } catch (JSONException e) {
        e.printStackTrace();
    }

        //ID represents the id for the record to update
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.PATCH, YOUR URL+ "?ID=" + ID, param,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                 //call handler to display response (optional)

                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            //call handler to display response (optional)

        }

    }) {

        //            Passing header
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            headers.put("x-api-key", "YOUR API KEY");
            return headers;
        }

        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {

           //use this to check positive response code returned else remove this override part

            return super.parseNetworkResponse(response);
        }
    };
    //and then you pass json data to volley
    VolleySingleton.getInstance(getActivity()).addToRequestQueue(jsonObjReq);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接