Android 如何修改标题?第二,共享参考
我正在制作一项服务,让用户能够登录LoginActivity,若登录成功,用户可以在PostActivity中发布一些东西。我正在使用Restful api 我在github上找到了很好的SharedReference示例: 及 AddCookiesInterceptor.javaAndroid 如何修改标题?第二,共享参考,android,sharedpreferences,retrofit,Android,Sharedpreferences,Retrofit,我正在制作一项服务,让用户能够登录LoginActivity,若登录成功,用户可以在PostActivity中发布一些东西。我正在使用Restful api 我在github上找到了很好的SharedReference示例: 及 AddCookiesInterceptor.java public class AddCookiesInterceptor implements Interceptor { public static final String PREF_COOKIES = "
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());
}
}
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;
}
}
它给了我HTTP/1.1 401错误,我发现这意味着头中有问题
我是这样写邮递服务的
public interface PostApiService {
@POST("posts/")
Call<ResponseBody> gettest(@Body TextData textData);}
公共接口邮递服务{
@POST(“posts/”)
调用gettest(@Body TextData);}
我的问题:
我想要你喜欢的头球
键:授权
//
值:令牌e0af91707f0434a1a2a7581dd3f4f483bdd717
我在哪里修改
我知道太多的密码可能会困扰你,所以我不得不说我很抱歉 制作一个拦截器来添加该头并将其添加到客户端怎么样?应该类似于您的
AddCookiesInterceptor
@nasch,谢谢您的建议,但我使用restapi重新生成json响应,json响应出现在主体上,而不是标题上。所以我做这件事有困难,因为我知识贫乏。请帮助我给出一些提示?您在向请求添加头或处理响应方面有问题吗?没有,我的问题是从rest api捕获响应。i、 e.我向api发出请求,如json{“id”:“any_value11”,“password”:“any_value22”},并获得json响应,如{“key”:“any_value33”}。我想使用或捕获{“key”:“any_value33}的any_value33
。在我的邮递员身上有{“key”:“any_value33}。不是标题。你能帮我吗事实上,昨天我在stackoverflow上问了这个问题之后,当我试图解决这个问题时,我的重要问题从上面的问题变为了。听起来你应该编辑你的问题以反映你当前的问题,或者问一个新问题。请随意发布一个新的链接。制作一个拦截器来添加该标题并将其添加到客户端如何?应该类似于您的AddCookiesInterceptor
@nasch,谢谢您的建议,但我使用restapi重新生成json响应,json响应出现在主体上,而不是标题上。所以我做这件事有困难,因为我知识贫乏。请帮助我给出一些提示?您在向请求添加头或处理响应方面有问题吗?没有,我的问题是从rest api捕获响应。i、 e.我向api发出请求,如json{“id”:“any_value11”,“password”:“any_value22”},并获得json响应,如{“key”:“any_value33”}。我想使用或捕获{“key”:“any_value33}的any_value33
。在我的邮递员身上有{“key”:“any_value33}。不是标题。你能帮我吗事实上,昨天我在stackoverflow上问了这个问题之后,当我试图解决这个问题时,我的重要问题从上面的问题变为了。听起来你应该编辑你的问题以反映你当前的问题,或者问一个新问题。请随意发布一个新链接。
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);
public interface PostApiService {
@POST("posts/")
Call<ResponseBody> gettest(@Body TextData textData);}