2017-09-25 47 views
0

我正在使用retrofit库来解析json.But中的数据,现在我需要在我的请求中传递令牌。我已经在一个全局类中设置了令牌。现在我想在APIClient中使用该令牌。但是当我传递时,它通过null。如何将令牌从java类传递到改进APIClient

这是我APIClient

public class APIClient { 
    public static final String BASE_URL = "http://kartpays.bizs/api/v5/"; 
    private static Retrofit retrofit = null; 
    public static final String FOLLOW_URL ="http://kartpays.biz/api/v1/follow/"; 

    public static Retrofit getRetrofitInstance() { 

     OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
     httpClient.addInterceptor(new Interceptor() { 
      @Override 
      public Response intercept(Interceptor.Chain chain) throws IOException { 
       Request originalRequest = chain.request(); 


       Request request = originalRequest.newBuilder() 
         .header("token", **I want to pass token here**) 
         .header("Content-Type", "application/json") 
         .method(originalRequest.method(), originalRequest.body()) 
         .build(); 

       return chain.proceed(request); 
      } 
     }); 
     OkHttpClient client = httpClient.build(); 

     retrofit = new Retrofit.Builder() 
       .baseUrl(FOLLOW_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .client(client) 
       .build(); 

     return retrofit; 
    } 
} 

在这个类我保持令牌

public class LibFile { 
    Context context; 

    public static SharedPreferences settings; 
    private static LibFile instance; 
    public static LibFile getInstance(Context context) { 
     if (instance == null) { 
      instance = new LibFile(context); 
     } 
     return instance; 
    } 

    public LibFile(Context context) { 
     this.context = context; 
     settings = context.getSharedPreferences(AppConstants.PREFS_FILE_NAME_PARAM, 0); 
    } 

    public String getUser_id() { 
     return settings.getString("user_id", ""); 
    } 

    public void setUser_id(String link) { 
     settings.edit().putString("user_id", link).commit(); //get link from here 
    } 

    public String getToken() { 
     return settings.getString("token","");//pass key here 
    } 

    public static void setToken(String userName) { 
     settings.edit().putString("token", userName).commit();//get key from here 
    } 

    public void clearCache() { 
     settings.edit().clear().commit(); 
     settings.edit().remove("link").commit(); 
    } 
} 

请建议我如何令牌动态传递。我不能错过静态令牌,因为它改变了提前

+1

,你在呼唤你的方法getRetrofitInstance(“令牌”) – gaurang

+0

如果我的答案是正确的,那么你也可以给予好评它,你可以通过它。这对其他人也是有用的。谢谢。 – Kuls

回答

0

continuously.Thanks如果您的令牌存储在SharedPreference然后检查你的代码的更改下方,

public static Retrofit getRetrofitInstance(Context mContext) { 

     SharedPreferences settings = mContext.getSharedPreferences(AppConstants.PREFS_FILE_NAME_PARAM, 0); 

     OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
     httpClient.addInterceptor(new Interceptor() { 
      @Override 
      public Response intercept(Interceptor.Chain chain) throws IOException { 
       Request originalRequest = chain.request(); 


       Request request = originalRequest.newBuilder() 
         .header("token", settings.getString("token", "")) 
         .header("Content-Type", "application/json") 
         .method(originalRequest.method(), originalRequest.body()) 
         .build(); 

       return chain.proceed(request); 
      } 
     }); 
     OkHttpClient client = httpClient.build(); 

     retrofit = new Retrofit.Builder() 
       .baseUrl(FOLLOW_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .client(client) 
       .build(); 

     return retrofit; 
    } 
} 
0

更好的解决方案相适应您的代码会是:

Request request = originalRequest.newBuilder() 
         .header("token", LibFile.getInstance(mContext).getToken()) 
         .header("Content-Type", "application/json") 
         .method(originalRequest.method(), originalRequest.body()) 
         .build(); 

检查这一行:.header("token", LibFile.getInstance(mContext).getToken())

内搭一个来自LibFile的实例并获取一个令牌。

0

在您的http请求中,标题键应该是“Authorization”而不是“token”。

Request request = originalRequest.newBuilder() 
        .header("Authorization", **I want to pass token here**) 
        .header("Content-Type", "application/json") 
        .method(originalRequest.method(), originalRequest.body()) 
        .build(); 

您可以使用下面的方法放置并从共享首选项获取令牌。

public static boolean setStringPreference(String key, String value) { 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 
    if (preferences != null && !TextUtils.isEmpty(key)) { 
     SharedPreferences.Editor editor = preferences.edit(); 
     editor.putString(key, value); 
     return editor.commit(); 
    } 
    return false; 
} 

public static String getStringPreference(String key) { 
    String value = null; 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 
    if (preferences != null) { 
     value = preferences.getString(key, null); 
    } 
    return value; 
}