我是一個新的程序員,我想用在改造Android的基於令牌的authorization.Therefore我也跟着教程(https://www.sitepoint.com/retrofit-a-simple-http-client-for-android-and-java/),它適用於我的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);
}
這裏是從i中使用的通信的碼(更新與方法):
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);
}
}
謝謝你,但如果在我的代碼,我得到令牌調用這個方法?我在我的文章中發佈了溝通者代碼。那裏的某個地方? – Peter
只需將方法放入您的班級,登錄後將通過授權進行調用。 – machei
嗯我沒有得到它,這個方法沒有返回任何東西,我怎麼能得到這個令牌?在我的帖子中看到更新,我把方法createService – Peter