2017-08-11 26 views
0

我很确定已经有一个答案我的问题,但我真的不明白他们的答案(并没有自信地改变太多我的代码x' ))从retrofit onResponse,检索一个字段的答案,并再次使用它

因此,如标题所示,我正在使用改造,我可以调用API,我有答案。 但在那之后,我想从答案中检索一个字段,以后使用它(比如做多的API调用需要一个令牌)

这里是问题代码:

Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl("https://api.orange.com/") 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 

     ApiService service = retrofit.create(ApiService.class); 

     String userName = "lCjTF83Ul61a0UXe0NSWcW459pnUDXQE"; 
     String passWord = "AGCEGvixfiAA513P"; 
     String base = userName + ":" + passWord; 

     String grant_type = "client_credentials"; 
     String authHeader = "Basic " + Base64.encodeToString(base.getBytes(), Base64.NO_WRAP); 
     Call<Authentification> authentificationCall = service.getTokenAccess(authHeader, grant_type); 

     authentificationCall.enqueue(new Callback<Authentification>() { 
      @Override 
      public void onResponse(Call<Authentification> call, Response<Authentification> response) { 
       int statusCode = response.code(); 
       Authentification authentification = response.body(); 
       Log.d(TAG, "onResponse: " + statusCode); 

      } 

      @Override 
      public void onFailure(Call<Authentification> call, Throwable t) { 
       Log.d(TAG, "onFailure: " + t.getMessage()); 

      } 


     }); 
    } 

而答案的代码:

public class Authentification { 

    @SerializedName("token_type") 
    @Expose 
    private String tokenType; 
    @SerializedName("access_token") 
    @Expose 
    private String accessToken; 
    @SerializedName("expires_in") 
    @Expose 
    private String expiresIn; 

    public String getTokenType() { 
     return tokenType; 
    } 

    public void setTokenType(String tokenType) { 
     this.tokenType = tokenType; 
    } 

    public String getAccessToken() { 
     return accessToken; 
    } 

    public void setAccessToken(String accessToken) { 
     this.accessToken = accessToken; 
    } 

    public String getExpiresIn() { 
     return expiresIn; 
    } 

    public void setExpiresIn(String expiresIn) { 
     this.expiresIn = expiresIn; 
    } 

}

所以在答案中有一个我想要的“access_token”字段。

我想要做这样的事情:(该Log.d在onResponse后)

final String abc = response.body().getAccessToken(); 

但后来我想用它在其他类,在其他改装实例

private void AppelReseauNRJ(){ 
    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl("https://api.orange.com/orangeradio/v1/radios/nrj/") 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    ApiRadioList service1 = retrofit.create(ApiRadioList.class); 



    String Zetoken = "Bearer " + ; 

    Call<GetStream> listRadioCall = service1.getRadio(Zetoken); 
    listRadioCall.enqueue(new Callback<GetStream>() { 
     @Override 
     public void onResponse(Call<GetStream> call, Response<GetStream> response) { 
      int statusCode = response.code(); 
      GetStream getStream = response.body(); 
      Log.d (TAG, "onResponse: " + statusCode); 
     } 

     @Override 
     public void onFailure(Call<GetStream> call, Throwable t) { 
      Log.d(TAG, "onFailure: " + t.getMessage()); 
     } 
    }); 
} 

(正是Zetoken是)

你能给我想到的一些线路,一些想法?

THX(我看午饭后您的答案)

回答

2

您可以保存在您的SharedPreference令牌,以后使用。

在登录API的结果,保存您的访问令牌

SharedPreferences preferences=getSharedPreferences("myPref",MODE_PRIVATE); 
SharedPreferences.Editor editor= preferences.edit(); 
editor.putString("token", response.body().getAccessToken()); 
editor.apply(); 

当过你需要(在其他活动)获取此令牌。

SharedPreferences preferences=getSharedPreferences("myPref", Context.MODE_PRIVATE); 
String token = preferences.getString("token",""); 
+0

我尝试你的解决方案,看起来不错,当我看着调试模式,我有良好的标题。 我需要解决出现的一个错误(java.lang.IllegalStateException:期望BEGIN_OBJECT,但是BEGIN_ARRAY在第1行第2列路径$“我会研究它,并回来给你,如果它的工作 Thx :) –

+0

所以我解决了我的问题,并且您的方法非常完美:D - THX –