簡體   English   中英

API Get 方法的共享首選項值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM