2016-04-09 16 views
0

"okhttp"で助けが必要です。私は、後でアプリでそれを再利用するという私の要求から得たクッキーを保存したい。私はこの例に遭遇しましたが、問題は私がどのパッケージにPreferencesクラスがあるか分かりません。どうすればインポートできますか?オートコンプリートを使用している場合はimport java.util.prefs.Preferences;を使用できます。しかし、それはアンドロイドではありません。それはgetDefaultPreferences()メソッドを含んでいません。以下のリンクのコードを参照してください。 AddCookiesInterceptor.javaでライン12上のOkHttp/Retrofitでクッキーを扱う - シンボル 'Preferences'を解決できません

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()); 
    } 
} 
+0

私の推測では、2つのタイプミスがあります。 'Preferences' - >' PreferenceManager'、 'getSharedPreferences()' - > 'getDefaultSharedPreferences()'のようなものです。 – CommonsWare

+0

@CommonsWare Preferencesは入力ミスではなく、コードをコピーして動作することが期待されています.-そこに書かれていても、どのように設定が変わるかは異なります。 – Tsuharesu

答えて

3

あなたはmSharedPreferencesメンバ変数を持つPreferenceManagerクラスを作成する必要があります。 mApplicationContext.getSharedPreferences(name, mode)を呼び出してmSharedPreferencesをインスタンス化します。

1
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(); 
+0

ReceivedCookiesInterceptorはありますか? – ReverseCold

+0

はい、プロジェクトにあります。 – charlesfranciscodev

関連する問題