[英]Retrofit Bearer token
我嘗試通過POST json {“ email”:“ test@example.com”,“ password”:“ test”}接收令牌。 在郵遞員中它起作用: 郵遞員請求 。 我嘗試在Android Studio中執行相同的操作。 我創建類令牌:
public class Token {
@SerializedName("token")
@Expose
public String token;
}
和類APIclient
class APIClient {
private static Retrofit retrofit = null;
static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl("http://mybaseurl.com/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
和接口APIInterface:
interface APIInterface {
@POST("/authenticate")
Call<Token> getLoginResponse(@Body AuthenticationRequest request);
}
和AuthenticationRequest類:
public class AuthenticationRequest {
String email;
String password;
}
在MainActivity的onCreate中:
apiInterface = APIClient.getClient().create(APIInterface.class);
authenticationRequest.email="test@example.com";
authenticationRequest.password="test";
getTokenResponse();
這是我的getTokenResponse方法:
private void getTokenResponse() {
Call<Token> call2 = apiInterface.getLoginResponse(authenticationRequest);
call2.enqueue(new Callback<Token>() {
@Override
public void onResponse(Call<Token> call, Response<Token> response) {
Token token = response.body();
}
@Override
public void onFailure(Call<Token> call, Throwable t) {
call.cancel();
}
});
}
這就是我在Logcat中看到的:
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: --> POST http://mybaseurl.com/authenticate http/1.1
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: Content-Type: application/json; charset=UTF-8
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: Content-Length: 46
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: {"email":"test@example.com","password":"test"}
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: --> END POST (46-byte body)
你能告訴我我在做什么錯嗎? 每當我想通過GET方法從服務器獲取信息時,都需要給令牌。 如何在Android代碼中接收和保存令牌?
您應該將application/json
添加到標頭
interface APIInterface {
@Headers({"Content-Type: application/json", "Accept: application/json"})
@POST("/authenticate")
Call<Token> getLoginResponse(@Body AuthenticationRequest request);
}
嘗試這個
interface APIInterface {
@POST("/authenticate")
Call<Token> getLoginResponse(@Header("Authorization") String token, @Body AuthenticationRequest request);
}
token
是不Bearer token
要從中獲取訪問令牌,您需要從響應中獲取標頭,並從標頭中讀取值。
Callback<User> user = new Callback<User>() {
@Override
public void success(User user, Response response) {
List<Header> headerList = response.getHeaders();
//iterating list of header and printing key/value.
for(Header header : headerList) {
Log.d(TAG, header.getName() + " " + header.getValue());
}
}
}
從標題即AccessToken
獲得所需的值后,可以將該值存儲在Storage中。 例如SharedPreference
因此,下次需要該值時,可以直接從存儲中獲取。 例如SharedPreference
現在,當您傳遞對任何Web服務的請求時,即GET
或POST
或任何其他方法。 您必須將其傳遞給標頭請求。
@GET("/tasks")
Call<List<Task>> getTasks(@Header("Content-Range") String contentRange);
或者,如果您每次都需要通過它,則可以直接從Retrofit
類中傳遞它。
Request request = original.newBuilder()
.header("User-Agent", "Your-App-Name")
.header("Accept", "application/vnd.yourapi.v1.full+json")
.method(original.method(), original.body())
.build();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.