Shared Preference value to API Get method

huangapple go评论107阅读模式
英文:

Shared Preference value to API Get method

问题

API接口

  1. @GET("/Account/{uid}/friends")
  2. Call<List<TblFriends>> getfriends(@Path("uid") String uid);

活动

  1. public void getFriends() {
  2. Api api = RetrofitClient.getInstance().create(Api.class);
  3. // 获取Shared Preference中的userID
  4. SharedPreferences preferences = getSharedPreferences(globalPreferenceName, MODE_PRIVATE);
  5. String uid = preferences.getString("token", ""); // 请替换为正确的key
  6. Call<List<TblFriends>> call = api.getfriends(uid);
  7. call.enqueue(new Callback<List<TblFriends>>() {
  8. @Override
  9. public void onResponse(Call<List<TblFriends>> call, Response<List<TblFriends>> response) {
  10. if (response.isSuccessful()) {
  11. List<TblFriends> tblFriends = response.body();
  12. friendsAdapter.setData(tblFriends);
  13. recyclerView.setAdapter(friendsAdapter);
  14. // Log.e("success",response.body().toString());
  15. }
  16. }
  17. @Override
  18. public void onFailure(Call<List<TblFriends>> call, Throwable t) {
  19. Log.e("failure",t.getLocalizedMessage());
  20. }
  21. });
  22. }

登录活动中的Shared Preference代码用于获取用户ID,这是完整的登录代码

  1. public static String globalPreferenceName = "profile";
  2. SharedPreferences.Editor editor = getSharedPreferences(globalPreferenceName, MODE_PRIVATE).edit();
  3. // 用户ID
  4. editor.putString("token", s);
  5. editor.putString("token2", JWTUtils.getJSon(token)); // 这一行可能需要根据实际情况修改
  6. 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

  1. @GET(&quot;/Account/{uid}/friends&quot;)
  2. Call&lt;List&lt;TblFriends&gt;&gt; getfriends(@Path(&quot;uid&quot;) String uid);

Activity

  1. public void getFriends()
  2. {
  3. Api api = RetrofitClient.getInstance().create(Api.class);
  4. Call&lt;List&lt;TblFriends&gt;&gt; call = api.getfriends();
  5. call.enqueue(new Callback&lt;List&lt;TblFriends&gt;&gt;() {
  6. @Override
  7. public void onResponse(Call&lt;List&lt;TblFriends&gt;&gt; call, Response&lt;List&lt;TblFriends&gt;&gt; response)
  8. {
  9. if (response.isSuccessful())
  10. {
  11. List&lt;TblFriends&gt; tblFriends = response.body();
  12. friendsAdapter.setData(tblFriends);
  13. recyclerView.setAdapter(friendsAdapter);
  14. // Log.e(&quot;success&quot;,response.body().toString());
  15. }
  16. }
  17. @Override
  18. public void onFailure(Call&lt;List&lt;TblFriends&gt;&gt; call, Throwable t) {
  19. Log.e(&quot;failure&quot;,t.getLocalizedMessage());
  20. }
  21. });
  22. }

Shared preference code is on login activity for to get userid ,this is the full code in login

  1. public static String globalPreferenceName = &quot;proofile&quot;;
  2. SharedPreferences.Editor editor =
  3. getSharedPreferences(globalPreferenceName,MODE_PRIVATE).edit();
  4. //UserID
  5. editor.putString(&quot;token&quot;,s);
  6. editor.putString(&quot;token2&quot;,JWTUtils.getJSon(token));
  7. editor.commit();

答案1

得分: 1

Sure, here's the translated content:

  1. // 获取名为 globalPreferenceName 的共享偏好设置如下。
  2. SharedPreferences sharedPref = context.getSharedPreferences(globalPreferenceName, Context.MODE_PRIVATE);
  3. // 使用 sharedPref 获取 token2 如下。
  4. String uid = sharedPref.getString("token2", defaultValue);
  5. // 然后将 uid 如下使用,
  6. Call<List<TblFriends>> call = api.getfriends(uid);
英文:
  1. Get your globalPreferenceName shared preference as below.
  2. SharedPreferences sharedPref = context.getSharedPreferences(globalPreferenceName,Context.MODE_PRIVATE);
  3. By using sharedPref get your token2 as below.
  4. String uid = sharedPref.getString(&quot;token2&quot;), defaultValue);
  5. Then use uid as below,
  6. Call&lt;List&lt;TblFriends&gt;&gt; call = api.getfriends(uid);

huangapple
  • 本文由 发表于 2020年9月12日 15:19:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63857913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定