我正在android中为HttpUrlConnection实现ResponseCache。一切似乎都有效。我打电话给
ResponseCache.setDefault(new ResCache());
我的ResCache类中的方法正在按预期使用。我唯一的问题是带请求参数的Map是空的。如果我在android环境之外尝试这个相同的代码,我将能够看到请求Header参数,但是在一个活动中,所有这些东西都无法正常工作。以下是活动的代码。
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环境之外。
对此有何帮助?
谢谢!
答案 0 :(得分:2)
我认为Android的HttpURLConnection实现中存在一个错误,其中响应头被传递给此方法而不是请求头。
我已经在我的代码中解决了这个问题,如下所示,虽然我没有google了解android中的bug的更多细节,但我相信android的httpurlconnection实现有一些问题,因为你可以看到它适用于非-android jvms:
基本上我通过传递VirenRequestHeadersMap而不是传递URLConnection#getRequestProperies()来修复它,以便与put(...)传递的值保持一致。
@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类:
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();
}
}