API Get 方法的共享首选项值
Shared Preference value to API Get method
我是 android 的新手,retrofit.i 希望将存储在共享首选项中的用户 ID 获取到 api 调用中。
我开发了 this.how 来用共享偏好值
替换 {uid}
API 界面
@GET("/Account/{uid}/friends")
Call<List<TblFriends>> getfriends(@Path("uid") String uid);
Activity
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());
}
});
}
Shared preference code is on login activity 用于获取 userid,这是登录中的完整代码
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);
我是 android 的新手,retrofit.i 希望将存储在共享首选项中的用户 ID 获取到 api 调用中。 我开发了 this.how 来用共享偏好值
替换 {uid}API 界面
@GET("/Account/{uid}/friends")
Call<List<TblFriends>> getfriends(@Path("uid") String uid);
Activity
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());
}
});
}
Shared preference code is on login activity 用于获取 userid,这是登录中的完整代码
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);