[英]How to access a string value of a shared preference from a method of another class
[英]Shared Preference value to API Get method
我是 android 和改造的新手。我想将存储在共享首选项中的用户 ID 存储到 api 调用中。 我已经开发了 this.how 用共享偏好值替换 {uid}
API接口
@GET("/Account/{uid}/friends")
Call<List<TblFriends>> getfriends(@Path("uid") String uid);
活动
public void getFriends()
{
Api api = RetrofitClient.getInstance().create(Api.class);
Call<List<TblFriends>> call = api.getfriends();
call.enqueue(new Callback<List<TblFriends>>() {
@Override
public void onResponse(Call<List<TblFriends>> call, Response<List<TblFriends>> response)
{
if (response.isSuccessful())
{
List<TblFriends> tblFriends = response.body();
friendsAdapter.setData(tblFriends);
recyclerView.setAdapter(friendsAdapter);
// Log.e("success",response.body().toString());
}
}
@Override
public void onFailure(Call<List<TblFriends>> call, Throwable t) {
Log.e("failure",t.getLocalizedMessage());
}
});
}
共享首选项代码在登录活动中用于获取用户 ID,这是登录中的完整代码
public static String globalPreferenceName = "proofile";
SharedPreferences.Editor editor =
getSharedPreferences(globalPreferenceName,MODE_PRIVATE).edit();
//UserID
editor.putString("token",s);
editor.putString("token2",JWTUtils.getJSon(token));
editor.commit();
Get your globalPreferenceName shared preference as below.
SharedPreferences sharedPref = context.getSharedPreferences(globalPreferenceName,Context.MODE_PRIVATE);
By using sharedPref get your token2 as below.
String uid = sharedPref.getString("token2"), defaultValue);
Then use uid as below,
Call<List<TblFriends>> call = api.getfriends(uid);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.