如何修改标题?改造2,共享偏好

时间:2016-09-10 20:54:40

标签: android sharedpreferences retrofit

我正在制作使用户能够登录LoginActivity的服务,如果登录成功,用户可以在PostActivity中发布内容。我正在使用Restful api。

我在github上找到了很好的共享偏好示例:https://gist.github.com/nikhiljha/52d45ca69a8415c6990d2a63f61184ffhttps://gist.github.com/tsuharesu/cbfd8f02d46498b01f1b

AddCookiesInterceptor.java

public class AddCookiesInterceptor implements Interceptor {
    public static final String PREF_COOKIES = "PREF_COOKIES";

    private Context context;

    public AddCookiesInterceptor(Context context) {
        this.context = context;
    }

    @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);
        }

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

RecievedCookiesInterceptor.java

public class ReceivedCookiesInterceptor implements Interceptor {
    private Context context;
    public ReceivedCookiesInterceptor(Context context) {
        this.context = context;
    } 
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());

        if (!originalResponse.headers("Set-Cookie").isEmpty()) {
            HashSet<String> cookies = (HashSet<String>) PreferenceManager.getDefaultSharedPreferences(context).getStringSet("PREF_COOKIES", new HashSet<String>());
            for (String header : originalResponse.headers("Set-Cookie")) {
                cookies.add(header);
            }
            SharedPreferences.Editor memes = PreferenceManager.getDefaultSharedPreferences(context).edit();
            memes.putStringSet("PREF_COOKIES", cookies).apply();
            memes.commit();
        }
        return originalResponse;
    }
}

我在这样的LoginActivity上使用了这段代码,

OkHttpClient client = new OkHttpClient();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.interceptors().add(new AddCookiesInterceptor(getApplicationContext()));
builder.interceptors().add(new RecievedCookiesInterceptor(getApplicationContext()));
client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(LoginApiService.Login_API_URL)
        .build();

loginApiService = retrofit.create(LoginApiService.class);

Call<ResponseBody> getkey = loginApiService.getkey(loginData);
getkey.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        if(response.code() == 200) {
            startActivity(new Intent(LoginActivity.this, PostActivity.class));
        }
    }

但在PostActivity中,(来自if (response.code() == 200) startActivity

我用过这个

    OkHttpClient client = new OkHttpClient();
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.interceptors().add(new AddCookiesInterceptor(getApplicationContext()));
#just used AddCookiesInterceptor, Not RecievedCookiesInterceptor

    client = builder.build();

    Retrofit retrofit = new Retrofit.Builder()
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(PostApiService.Post_API_URL)
            .build();
    PostApiService = retrofit.create(PostApiService.class);

它给我HTTP / 1.1 401错误,我发现这意味着标题中存在问题。

我像这样编写PostApiService

public interface PostApiService {
    @POST("posts/")
    Call<ResponseBody> gettest(@Body TextData textData);}

我的问题:

我想要像Header一样 key : Authorization // value : Token e0af91707f0434a1a2a7581dd3f4f483bdd717

我在哪里修改?

我知道太多代码可能会让你烦恼,所以我不得不说我非常抱歉。

0 个答案:

没有答案