gpt4 book ai didi

android - 在 header 中发送 token (不同的 Activity )

转载 作者:搜寻专家 更新时间:2023-11-01 09:20:53 46 4
gpt4 key购买 nike

我在标题中发送 token 并在授权后从网站获取一些信息时遇到问题。

这是我的 MainActivity.java:

    public class MainActivity extends AppCompatActivity {

public static final String BASE_URL = "https://api.some.some/v1/";

SomeAPI userClient;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

userClient = retrofit.create(SomeAPI.class);

login();

}

private void login() {
Auth login = new Auth("123456", "12345678");
Call<User> call = userClient.login(login);

call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.code() == 201) {
Toast.makeText(MainActivity.this, response.body().getToken(), Toast.LENGTH_SHORT).show();
token = response.body().getToken();
}
else {
Toast.makeText(MainActivity.this, "Token is not truth :(", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Call<User> call, Throwable t) {
Toast.makeText(MainActivity.this, "error!", Toast.LENGTH_SHORT).show();
}
});
}

}

这是我的一些 API 接口(interface):

public interface SomeAPI {

@POST("token")
Call<User> login(@Body Auth login);

}
Here is my Auth.java:

public class Auth {
private String login;
private String password;

public Auth(String login, String password) {
this.login = login;
this.password = password;
}
}

这是我的 User.java:

    public class User {
private int id;
private String email;
private String token;

public int getId(){
return id;
}

public void setId(){
this.id = id;
}
public String getEmail(){
return email;
}

public void setEmail(String email){
this.email = email;
}
public String getToken(){return token;}
public void setToken(String token){this.token = token;}
public String getfirst_name()
{
return first_name;
}
public void setfirst_name(String first_name) {
this.first_name = first_name;
}

}

所以在这之后我有了一个 token ,现在我想在我的其他 Activity 中获得一些信息。

用户配置文件.java:

    ProstoTVAPI userClient;

String first_name = "";

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

userClient = retrofit.create(ProstoTVAPI.class);

getUserInfo();
}
private void getUserInfo() {
User user = new User();
Call<User> call = userClient.getInfo();

call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.code() == 200){
first_name = response.body().getfirst_name();
tvName.setText(first_name);


}
else {
Toast.makeText(UserProfile.this, "First name was not loaded", Toast.LENGTH_SHORT).show();

}
}

@Override
public void onFailure(Call<User> call, Throwable t) {
Toast.makeText(UserProfile.this, "error!", Toast.LENGTH_SHORT).show();
}
});
}

我必须发送授权后获得的 token 以获取新信息,例如 first_name = response.body().getfirst_name();

我怎样才能从这个 Activity 中做到这一点?我需要在我的代码中更改/添加什么?

更新:

我已经创建了 PreferencesManager 来保存我的首选项:

public class PreferencesManager {

private static final String PREFERENCES = "MyPrefs";
private static PreferencesManager instance = null;
private static SharedPreferences sharedPreferences;

public static PreferencesManager getInstance() {
if (instance == null) {
instance = new PreferencesManager();
}
return instance;
}

private static SharedPreferences getSharedPreferences(Context context) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
}
return sharedPreferences;
}


public static void putString(Context context, String key, String value) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value).apply();
}

public static String getString(Context context, String key, String defValue) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
return sharedPreferences.getString(key, defValue);
}


}

所以在我获得 token 的 Activity 中,我做了:editor.putString("token", token);,其中编辑器 - SharedPreferences.Editor editor;

在 Activity 中,我将 token 作为 header 发送:

    private void getUserInfo() {
User user = new User();

Call<User> call = userClient.getInfo("Bearer " + token);
...
}

在 onCreate() 中:

editor = getSharedPreferences("MyPrefs", MODE_PRIVATE).edit();
token = PreferencesManager.getString(this,"token", null);

我还添加到 ApiInterface 中:

Call<User> getInfo(@Header("Authorization") String token);

最佳答案

我以这种方式在 header 中发送 token :-

这是 ApiInterface 类:-

Call<ReactionResponse> submitReactionPart(@Header("Authorization") String token);

这是 Activity :-

Call<ReactionResponse> responseCall = apiInterface.submitReactionPart("Bearer " +token);

这是我的偏好,请看一看:-

    public class AppPrefrences {

private static SharedPreferences mPrefs;
private static SharedPreferences.Editor mPrefsEditor;

public static boolean isUserLoggedOut(Context ctx) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
return mPrefs.getBoolean("id_logged_in", true);
}

public static void setUserLoggedOut(Context ctx, Boolean value) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
mPrefsEditor = mPrefs.edit();
mPrefsEditor.putBoolean("id_logged_in", value);
mPrefsEditor.commit();
}

public static String getUserImage(Context ctx) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
return mPrefs.getString("userImage", "");
}

public static void setUserImage(Context ctx, String value) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
mPrefsEditor = mPrefs.edit();
mPrefsEditor.putString("userImage", value);
mPrefsEditor.commit();
}

在要使用首选项值的 Activity 中,它将返回字符串值:-

String userImageUrl = AppPreference.getUserImage(MyActivity.class);

在您想要优先存储您的值(value)的 Activity 中:-

AppPreference.setUserImage(MyActivity.class, "put your value here");

关于android - 在 header 中发送 token (不同的 Activity ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55645879/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com