首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓和ResponseCache。请求标头丢失

安卓和ResponseCache。请求标头丢失
EN

Stack Overflow用户
提问于 2011-07-01 05:28:23
回答 1查看 2.1K关注 0票数 1

我正在安卓系统中为HttpUrlConnection实现ResponseCache。一切似乎都很好用。我打电话给

代码语言:javascript
复制
ResponseCache.setDefault(new ResCache());

并且我的ResCache类中的方法可以按预期使用。我遇到的唯一问题是带有请求参数的Map是空的。如果我在android环境之外尝试相同的代码,我将能够看到请求头参数,但在活动内部,所有这些东西都不起作用。以下是该活动的代码。

代码语言:javascript
复制
package com.httptest;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.CacheRequest;
import java.net.CacheResponse;
import java.net.HttpURLConnection;
import java.net.ResponseCache;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;

public class HttpTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ResponseCache.setDefault(new ResCache());
        HttpURLConnection urlConnection = null;
        try {
        URL url = new URL("http://169.254.198.146//patch/500.php");
        urlConnection = (HttpURLConnection) url
                .openConnection();
        urlConnection.setUseCaches(true);
        urlConnection.setRequestProperty("MY_PROPERTY", "MY_VALUE");
            InputStream in = new BufferedInputStream(
                    urlConnection.getInputStream());
            String aux = readStream(in);
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            urlConnection.disconnect();
        }
    }

    public static String readStream(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000);
        for (String line = r.readLine(); line != null; line = r.readLine()) {
            sb.append(line);
        }
        in.close();
        return sb.toString();
    }

    public class ResCache extends ResponseCache{

        @Override
        public CacheResponse get(URI arg0, String arg1,
                Map<String, List<String>> arg2) throws IOException {
            System.out.println(arg2);
            return null;
        }

        @Override
        public CacheRequest put(URI uri, URLConnection connection)
                throws IOException {
            return null;
        }
    }
}

在方法ResCache.get(URI,String,Map)中,映射始终为空。我想看看我添加到请求中的参数。顺便说一句,参数都在请求中设置得很好,因为我可以在执行请求时在服务器中读取它们。

同样,这可以在android环境之外工作。

对此有什么帮助吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-14 07:01:57

我认为在Android的HttpURLConnection实现中存在一个错误,即响应报头被传递给此方法,而不是请求报头。

我已经在下面的代码中解决了这个问题,尽管我没有在谷歌上搜索到android中的bug的更多细节,但我非常确信android的httpurlconnection实现存在一些问题,因为你可以看到它在非android jvm上工作:

基本上,我通过传递一个VirenRequestHeadersMap而不是传递URLConnection#getRequestProperies()来修复它,使其与从put(...)传递的值保持一致。

代码语言:javascript
复制
@Override
        public CacheRequest put(URI uri, URLConnection connection)
                throws IOException {
            MyPrivateContext context = <>;
            context.setConnection(connection);

            //...
        }

@Override
        public CacheResponse get(URI arg0, String arg1,
                Map<String, List<String>> arg2) throws IOException {
            System.out.println(arg2);

            MyPrivateContext context = <>;
            URLConnection connection = context.getConnection();
            arg2 = new VirenRequestHeadersMap(connection);
            //...
        }

下面是整个VirenRequestHeadersMap类,如果您对它的外观感兴趣的话:

代码语言:javascript
复制
class VirenRequestHeadersMap implements Map<String, List<String>> {

    private final URLConnection mConnection;

    public VirenRequestHeadersMap(URLConnection connection) {
        mConnection = connection;
    }

    public void clear() {
        throw new UnsupportedOperationException();
    }

    public boolean containsKey(Object key) {
        if (key instanceof String) {
            String field = (String) key;
            return mConnection.getRequestProperty(field) != null;
        } else {
            return false;
        }
    }

    public boolean containsValue(Object value) {
        throw new UnsupportedOperationException();
    }

    public Set<Entry<String, List<String>>> entrySet() {
        throw new UnsupportedOperationException();
    }

    public List<String> get(Object key) {
        if (key instanceof String) {
            String field = (String) key;
            String value = mConnection.getRequestProperty(field);
            return value != null ? Collections.singletonList(value) : null;
        } else {
            return null;
        }
    }

    public boolean isEmpty() {
        throw new UnsupportedOperationException();
    }

    public Set<String> keySet() {
        throw new UnsupportedOperationException();
    }

    public List<String> put(String key, List<String> value) {
        throw new UnsupportedOperationException();
    }

    public void putAll(Map<? extends String, ? extends List<String>> value) {
        throw new UnsupportedOperationException();
    }

    public List<String> remove(Object key) {
        throw new UnsupportedOperationException();
    }

    public int size() {
        throw new UnsupportedOperationException();
    }

    public Collection<List<String>> values() {
        throw new UnsupportedOperationException();
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6541307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档