我是一名新程序员,我想在android中使用Retrofit进行基于令牌的授权。因此,我遵循了一个教程(https://www.sitepoint.com/retrofit-a-simple-http-client-for-android-and-java/)并将其改编为我的ruby on rails服务器(在教程中它是一个PHP的服务器)。但是,我不知道令牌在程序中的保存位置或如何获得它。它只出现在Android控制台中。我需要在我的android程序中解码令牌。但是令牌只出现在我的Android控制台中:
com.example.programmingknowledge.login D/Retrofit: {"auth_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJuYW1lIjpudWxsLCJlbWFpbCI6ImV4YW1wbGVAbWFpbC5jb20iLCJleHAiOjE0NzMwMzU1MTF9.bhJABvEgtRISZVJ0DejjEtrGJyXzNV6PO0YGQ71xHRc"}
如何在Android应用中获取令牌?任何的想法?
这是我的界面:
public interface Interface {
//This method is used for "POST"
@FormUrlEncoded
//@POST("/api.php")
@POST("/")
void postData(@Field("method") String method,
@Field("email") String username,
@Field("password") String password,
Callback<ServerResponse> serverResponseCallback);
//This method is used for "GET"
//@GET("/api.php")
@GET("/")
void getData(@Query("method") String method,
@Query("email") String username,
@Query("password") String password,
Callback<ServerResponse> serverResponseCallback);
}
这是我使用的通信器的代码(用方法更新):
import android.util.Log;
import com.squareup.otto.Produce;
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
public class Communicator {
private static final String TAG = "Communicator";
private static final String SERVER_URL = "http://10.0.2.2:3000/authenticate";
public void loginPost(String email, String password){
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(SERVER_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
Interface communicatorInterface = restAdapter.create(Interface.class);
//HERE I PLACED THE METHOD, BUT HOW CAN I GET THE AUTH-TOKEN WITH THIS METHOD? I want to save it somewhere
communicatorInterface = ServiceGenerator.createService(Interface.class, "auth-token");
Callback<ServerResponse> callback = new Callback<ServerResponse>() {
@Override
public void success(ServerResponse serverResponse, Response response2) {
if(serverResponse.getResponseCode() == 0){
BusProvider.getInstance().post(produceServerEvent(serverResponse));
}else{
BusProvider.getInstance().post(produceErrorEvent(serverResponse.getResponseCode(), serverResponse.getMessage()));
}
}
@Override
public void failure(RetrofitError error) {
if(error != null ){
Log.e(TAG, error.getMessage());
error.printStackTrace();
}
BusProvider.getInstance().post(produceErrorEvent(-200,error.getMessage()));
}
};
communicatorInterface.postData("login", email, password, callback);
}
public void loginGet(String email, String password){
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(SERVER_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
Interface communicatorInterface = restAdapter.create(Interface.class);
Callback<ServerResponse> callback = new Callback<ServerResponse>() {
@Override
public void success(ServerResponse serverResponse, Response response2) {
if(serverResponse.getResponseCode() == 0){
BusProvider.getInstance().post(produceServerEvent(serverResponse));
}else{
BusProvider.getInstance().post(produceErrorEvent(serverResponse.getResponseCode(), serverResponse.getMessage()));
}
}
@Override
public void failure(RetrofitError error) {
if(error != null ){
Log.e(TAG, error.getMessage());
error.printStackTrace();
}
BusProvider.getInstance().post(produceErrorEvent(-200,error.getMessage()));
}
};
communicatorInterface.getData("login", email, password, callback);
}
@Produce
public ServerEvent produceServerEvent(ServerResponse serverResponse) {
return new ServerEvent(serverResponse);
}
@Produce
public ErrorEvent produceErrorEvent(int errorCode, String errorMsg) {
return new ErrorEvent(errorCode, errorMsg);
}
}
这是我的ServiceGenerator类
public class ServiceGenerator {
public static final String API_BASE_URL = "https://your.api-base.url";
private static RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint(API_BASE_URL)
.setClient(new OkClient(new OkHttpClient()));
public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null);
}
public static <S> S createService(Class<S> serviceClass, final String authToken) {
if (authToken != null) {
builder.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Authorization", authToken);
}
});
}
RestAdapter adapter = builder.build();
return adapter.create(serviceClass);
}
}
答案 0 :(得分:0)
看一下教程:https://futurestud.io/blog/retrofit-token-authentication-on-android
然后去那样:
$.ajax({
url: imageUrl,
type: 'DELETE',
crossDomain: true
});
答案 1 :(得分:0)
从服务器返回的改造日志json结果...
正如您所看到的,令牌已从您需要获取它的服务器返回。它应该在ServerResponse对象中(如果您正确命名参数),您可以通过获取响应正文来获取令牌。将其保存在静态变量或其他内容中,然后在createService
方法