gpt4 book ai didi

Android 和 ResponseCache...请求 header 丢失

转载 作者:行者123 更新时间:2023-11-29 22:23:35 26 4
gpt4 key购买 nike

我正在为 android 中的 HttpUrlConnection 实现 ResponseCache。一切似乎都有效。我叫

ResponseCache.setDefault(new ResCache());

我的 ResCache 类中的方法正在按预期使用。我遇到的唯一问题是带有请求参数的 map 是空的。如果我在 android 环境之外尝试这个完全相同的代码,我将能够看到请求 Header 参数,但在 Activity 中并且所有这些东西都不起作用。这是 Activity 的代码。

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) 中, map 始终为空。我想查看我添加到请求中的参数。顺便说一句,参数在请求中设置得很好,因为我可以在执行请求时在服务器中读取它们。

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

有什么帮助吗?

谢谢!

最佳答案

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

我已经在我的代码中解决了这个问题,如下所示,尽管我没有通过谷歌搜索有关 android 中错误的更多详细信息,但我非常确信 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();
}
}

关于Android 和 ResponseCache...请求 header 丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6541307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com