英文:
Shared Preference value to API Get method
问题
API接口
@GET("/Account/{uid}/friends")
Call<List<TblFriends>> getfriends(@Path("uid") String uid);
活动
public void getFriends() {
Api api = RetrofitClient.getInstance().create(Api.class);
// 获取Shared Preference中的userID
SharedPreferences preferences = getSharedPreferences(globalPreferenceName, MODE_PRIVATE);
String uid = preferences.getString("token", ""); // 请替换为正确的key
Call<List<TblFriends>> call = api.getfriends(uid);
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代码用于获取用户ID,这是完整的登录代码
public static String globalPreferenceName = "profile";
SharedPreferences.Editor editor = getSharedPreferences(globalPreferenceName, MODE_PRIVATE).edit();
// 用户ID
editor.putString("token", s);
editor.putString("token2", JWTUtils.getJSon(token)); // 这一行可能需要根据实际情况修改
editor.commit();
请注意,您需要根据您的实际情况替换代码中可能的占位符,如Shared Preference中存储userID的key和登录代码中的token相关操作。
英文:
I am new to android and retrofit.i want get userID stored in shared preference into api call.
i have developed this.how to replace {uid} with shared preference value
API interface
@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 for to get userid ,this is the full code in login
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();
答案1
得分: 1
Sure, here's the translated content:
// 获取名为 globalPreferenceName 的共享偏好设置如下。
SharedPreferences sharedPref = context.getSharedPreferences(globalPreferenceName, Context.MODE_PRIVATE);
// 使用 sharedPref 获取 token2 如下。
String uid = sharedPref.getString("token2", defaultValue);
// 然后将 uid 如下使用,
Call<List<TblFriends>> call = api.getfriends(uid);
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论