【问题标题】:Handling Cookies with OkHttp/Retrofit - Cannot resolve symbol 'Preferences'使用 OkHttp/Retrofit 处理 Cookie - 无法解析符号“首选项”
【发布时间】:2016-04-12 23:27:18
【问题描述】:

嘿,我需要一些关于“okhttp”的帮助。我想存储从我的请求中获得的 cookie,以便稍后在应用程序中重用它。我遇到了这个例子,但问题是我不知道Preferences 类在哪个包中。我怎样才能导入它?如果我使用自动完成,我可以使用import java.util.prefs.Preferences;。但它不是安卓的。它不包含getDefaultPreferences() 方法。请参阅下面链接中的代码。

AddCookiesInterceptor.java 的第 12 行:

HashSet<String> preferences = (HashSet) Preferences
    .getDefaultPreferences()
    .getStringSet(Preferences.PREF_COOKIES, new HashSet<>());

http://tsuharesu.com/handling-cookies-with-okhttp/

/**
 * This interceptor put all the Cookies in Preferences in the Request.
 * Your implementation on how to get the Preferences MAY VARY.
 * <p>
 * Created by tsuharesu on 4/1/15.
 */
public class AddCookiesInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        HashSet<String> preferences = (HashSet) Preferences.getDefaultPreferences().getStringSet(Preferences.PREF_COOKIES, new HashSet<>());
        for (String cookie : preferences) {
            builder.addHeader("Cookie", cookie);
            Log.v("OkHttp", "Adding Header: " + cookie); // This is done so I know which headers are being added; this interceptor is used after the normal logging of OkHttp
        }

        return chain.proceed(builder.build());
    }
}

【问题讨论】:

  • 我猜你看到了两个错别字。 Preferences -> PreferenceManagergetSharedPreferences() -> getDefaultSharedPreferences()
  • @CommonsWare Preferences 不是拼写错误,他只是复制了代码并希望能正常工作——尽管那里写着他获取 Preferences 的方式可能会有所不同

标签: java android cookies


【解决方案1】:

您需要创建一个具有mSharedPreferences 成员变量的PreferenceManager 类。通过调用mApplicationContext.getSharedPreferences(name, mode) 实例化mSharedPreferences

【讨论】:

    【解决方案2】:
    package com.trainerworkout.trainerworkout.network;
    
    import android.content.Context;
    import android.preference.PreferenceManager;
    import android.util.Log;
    
    import java.io.IOException;
    import java.util.HashSet;
    
    import okhttp3.Interceptor;
    import okhttp3.Request;
    import okhttp3.Response;
    
    /**
     * This interceptor put all the Cookies in Preferences in the Request.
     * Your implementation on how to get the Preferences MAY VARY.
     */
    public class AddCookiesInterceptor implements Interceptor {
        public static final String PREF_COOKIES = "PREF_COOKIES";
        private Context context;
    
        public AddCookiesInterceptor(Context context) {
            this.context = context;
        } // AddCookiesInterceptor()
    
        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request.Builder builder = chain.request().newBuilder();
            HashSet<String> preferences = (HashSet<String>) PreferenceManager.getDefaultSharedPreferences(context).getStringSet(PREF_COOKIES, new HashSet<String>());
            for (String cookie : preferences) {
                builder.addHeader("Cookie", cookie);
                Log.v("OkHttp", "Adding Header: " + cookie); // This is done so I know which headers are being added; this interceptor is used after the normal logging of OkHttp
            } // for
    
            return chain.proceed(builder.build());
        } // intercept()
    } // AddCookiesInterceptor
    

    您在某个地方创建了一个新的 OkHttpClient 并将其用于您的所有请求。

    public static OkHttpClient client = new OkHttpClient();
    private OkHttpClient.Builder builder = new OkHttpClient.Builder();
    
    builder.addInterceptor(new AddCookiesInterceptor(context));
    builder.addInterceptor(new ReceivedCookiesInterceptor(context));
    client = builder.build();
    

    【讨论】:

    • 你有ReceivedCookiesInterceptor吗?
    • 是的,在项目中。
    猜你喜欢
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 2018-07-25
    • 2018-11-11
    • 2019-04-28
    • 1970-01-01
    相关资源
    最近更新 更多