Shared Preference value to API Get method

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

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(&quot;/Account/{uid}/friends&quot;)
        Call&lt;List&lt;TblFriends&gt;&gt; getfriends(@Path(&quot;uid&quot;) String uid);

Activity

 public void getFriends()
   {
    Api api = RetrofitClient.getInstance().create(Api.class);

    Call&lt;List&lt;TblFriends&gt;&gt; call = api.getfriends();
    call.enqueue(new Callback&lt;List&lt;TblFriends&gt;&gt;() {
        @Override
        public void onResponse(Call&lt;List&lt;TblFriends&gt;&gt; call, Response&lt;List&lt;TblFriends&gt;&gt; response)

        {

            if (response.isSuccessful())
            {
                List&lt;TblFriends&gt; tblFriends = response.body();
                friendsAdapter.setData(tblFriends);
                recyclerView.setAdapter(friendsAdapter);

                //    Log.e(&quot;success&quot;,response.body().toString());
            }

        }
        @Override
        public void onFailure(Call&lt;List&lt;TblFriends&gt;&gt; call, Throwable t) {

            Log.e(&quot;failure&quot;,t.getLocalizedMessage());
        }
    });
}

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

public static String globalPreferenceName = &quot;proofile&quot;;


        SharedPreferences.Editor editor = 
        getSharedPreferences(globalPreferenceName,MODE_PRIVATE).edit();
       //UserID
        editor.putString(&quot;token&quot;,s);  
       editor.putString(&quot;token2&quot;,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(&quot;token2&quot;), defaultValue);

Then use uid as below,
 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:

确定