我有一个用于生成访客 token 的 API
即 {{url}}/api/token/guest
返回 token 为:
{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjIxNzc0NTI3OTksImlhdCI6MTs4Xk3x-zFfDg5mhYJO7jM8"}
现在我想调用 guest token 并附加到我的 session 中,以便我可以访问 api 中的数据
即
@GET("/api/properties-latest")
Call<Datum> getNewProperties(@Header("Authorization") String token);
我的 session 类:
public class Session {
Context context;
private SharedPreferences prefs;
private Session session;
private ApiInterface apiInterface;
public Session(Context cntx) {
// TODO Auto-generated constructor stub
this.context = cntx;
prefs = PreferenceManager.getDefaultSharedPreferences(context);
}
public void setJwtToken(String token) {
prefs.edit().putString("token", token).commit();
}
public String getJwtToken() {
String token = prefs.getString("token", "");
if (token == null || token.isEmpty()) {
token = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjIxNzc0NTI3OTksImlhdCI6MTUxNjAyMjk5OSwiaXNzIjoiQmFzb2JhYXMgTmVwYWwiLCJuYmYiOjE1MTYwMjI5OTksImp0aSI6Ikd1ZXN0VG9rZW4iLCJzdWIiOjB9.QikmNgBYmqch5HREGFEpUs4Xk3x-zFfDg5mhYJO7jM8";
}
return token;
}
public String getRefreshToken() {
String token = prefs.getString("RefreshToken", "");
if (token == null || token.isEmpty()) {
token = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjIxNzc0NTI3OTksImlhdCI6MTUxNjAyMjk5OSwiaXNzIjoiQmFzb2JhYXMgTmVwYWwiLCJuYmYiOjE1MTYwMjI5OTksImp0aSI6Ikd1ZXN0VG9rZW4iLCJzdWIiOjB9.QikmNgBYmqch5HREGFEpUs4Xk3x-zFfDg5mhYJO7jM8";
}
return token;
}
public void setRefreshToken(String token) {
prefs.edit().putString("RefreshToken", token).commit();
}
}
我的属性方法:
property_recyclerView = view.findViewById(R.id.new_property_recyclerview);
property_recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
property_recyclerView.setHasFixedSize(true);
property_recyclerView.setItemAnimator(new DefaultItemAnimator());
listener = this;
Call<Datum> propertyCall = apiInterface.getNewProperties(session.getJwtToken());
propertyCall.enqueue(new Callback<Datum>() {
@Override
public void onResponse(Call<Datum> call, Response<Datum> response) {
if (response.code() == 200) {
datum = response.body();
List<Property> propertyList = datum.getPropertyList();
propertyAdapter = new PropertyAdapter(propertyList, getContext(), listener);
property_recyclerView.setAdapter(propertyAdapter);
}
}
@Override
public void onFailure(Call<Datum> call, Throwable t) {
Log.e("New Property Fragment", t.getMessage());
}
});
return view;
}
我想访问 guest token ,以便 guest 用户可以查看属性,请帮我找到解决方案。
最佳答案
使用 context
访问 SharedPreference
,如下所示:
import static android.content.Context.MODE_PRIVATE;
class MyPref {
private SharedPreferences sp;
private SharedPreferences.Editor spe;
Context context;
public MyPref (Context cntx) {
// TODO Auto-generated constructor stub
this.context = cntx;
sp = context.getSharedPreferences("MyData", MODE_PRIVATE);
spe = sp.edit();
}
public void setJwtToken(String token) {
spe.putString("token", token).apply();
}
public String getJwtToken() {
String token = sp.getString("token", "");
if (token == null || token.isEmpty()) {
token = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjIxNzc0NTI3OTksImlhdCI6MTUxNjAyMjk5OSwiaXNzIjoiQmFzb2JhYXMgTmVwYWwiLCJuYmYiOjE1MTYwMjI5OTksImp0aSI6Ikd1ZXN0VG9rZW4iLCJzdWIiOjB9.QikmNgBYmqch5HREGFEpUs4Xk3x-zFfDg5mhYJO7jM8";
}
return token;
}
}
现在从您的 Activity 中访问,如下所示:
MyPref m = new MyPref(this);
String tokenToUse = m.getJwtToken();
现在对于 Retrofit 调用,请像下面这样使用它:
Call<Datum> call = apiInterface.getNewProperties(tokenToUse);
关于android - 使用 Retrofit2 调用 API 来保存和检索 JWT Token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48922644/