2017-08-17 111 views
0

我通過Retrofit向服務器發送了一個post請求,但是當服務器發回響應時,我想從令牌獲取一個值。這是我如何發送我的帖子請求和onSuccess我想要接收令牌。我的問題在於,我不知道從服務器響應中檢索令牌。請在這裏幫忙。從JSON服務器響應中獲取令牌

public void loginUser(String userName, String userPassword, Boolean userRememberMe) { 

    mAPIService.loginUser(userName, userPassword, userRememberMe).enqueue(new Callback<Login>() { 
     @Override 
     public void onResponse(Response<Login> response, Retrofit retrofit) { 
      if(response.isSuccess()) { 
       //save token here 
       Intent intent = new Intent(LoginActivity.this, ProfileActivity.class); 
       startActivity(intent); 
      } 
     } 

     @Override 
     public void onFailure(Throwable throwable) { 
      Log.e(TAG, "Unable to Login"); 
     } 
    }); 
} 

這是我從服務器獲得響應

{ 
"total": 0, 
"data": { 
"id": "348680c7-d46b-4adc-9be0-89a1d1a38566", 
"username": "0242770336", 
"name": "name", 
"phoneNumber": "063546345", 
"email": "[email protected]", 
"dateOfBirth": null, 
"image": null, 
"gender": "", 
"idTypeId": null, 
"idType": null, 
"idNumber": null, 
"idExpiryDate": null, 
"idVerified": false, 
"cityId": null, 
"city": null, 
"residentialAddress": null, 
"latitude": 0, 
"longitude": 0, 
"createdAt": "2017-08-14T17:45:16.24Z", 
"role": { 
    "id": 2, 
    "name": "User", 
    "privileges": [ 
    "" 
    ] 
}, 
"token": "jNK_DGszYOMEpPOFoRGjuyJ5KX9kaJQKCl3cujlHoXklCS8Ij6b-QBmhv0jVwVC54KcNXkzyM62xpswqcjo9Ajf-n-rWzSaIoiYNglaXhtPspziZ0PcTKzTMAvw8si3A7BlcD98M-IjIxYjxieVYAPWVtcvomWi" 
}, 

"message": "Login Successfull", 
"success": true 
} 

這是我的登錄模式

public class Login { 

@SerializedName("userName") 
@Expose 
private String userName; 
@SerializedName("password") 
@Expose 
private String password; 
@SerializedName("rememberMe") 
@Expose 
private Boolean rememberMe; 

public String getUserName() { 
    return userName; 
} 

public void setUserName(String userName) { 
    this.userName = userName; 
} 

public String getPassword() { 
    return password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 

public Boolean getRememberMe() { 
    return rememberMe; 
} 

public void setRememberMe(Boolean rememberMe) { 
    this.rememberMe = rememberMe; 
} 

}

回答

3

有兩種方法。

方法1:使用的JSONObject

mAPIService.loginUser(userName, userPassword, userRememberMe).enqueue(new Callback<JsonObject>() { 
    @Override 
public void onResponse(Response<JsonObject> response, Retrofit retrofit) { 
     if(response.isSuccess()) { 
     try { 

      String token=response.body().get("data").get("token"); 
     } catch (JSONException e) { 
       e.printStackTrace(); 
     } 
Intent intent = new Intent(LoginActivity.this,ProfileActivity.class); 
startActivity(intent); 
    } 
} 
} 

方法2: 可以使用http://pojo.sodhanalibrary.com/對POJO您迴應轉換。現在把它添加到您的項目,讓我們將其命名爲ResponseData.class並獲得令牌使用getter方法從做以下的第一選項更改

mAPIService.loginUser(userName, userPassword, userRememberMe).enqueue(new Callback<ResponseData>() { 
    @Override 
    public void onResponse(Response<ResponseData> response, Retrofit retrofit) { 
     if(response.isSuccess()) { 
      //save token here 
String token =response.getData().getToken(); 

      Intent intent = new Intent(LoginActivity.this, ProfileActivity.class); 
      startActivity(intent); 
     } 
    } 

    @Override 
    public void onFailure(Throwable throwable) { 
     Log.e(TAG, "Unable to Login"); 
    } 
}); 
+0

如果回調是在登錄模式排隊,我將有一個錯誤。你在這種情況下建議我做什麼。 –

+0

我的請求模型與我的響應模型不同。檢查我更新的問題。 –

+0

回調將響應模型作爲參數,因此您無法在其中使用登錄模型,因爲您的響應與響應不同。你需要改變的是你的改造請求應該返回調用或調用而不是調用

0

轉換的響應串入JSONObject,然後再得到JSONObject的數據和數據可以找到你的令牌對象。

JSONObject object = new JSONObject(response); 
JSONObject dataObject = object.getJSONObject("data"); 
String token = dataObject.getString("token"); 
Log.i("TAG","token is "+token); 
0

試試這個。

使用optBoolean()方法和optString()方法

try { 
     JSONObject jsonObject = new JSONObject(response); 
     boolean success = jsonObject.optBoolean("success"); 
     if (success) { 
      JSONObject data = jsonObject.getJSONObject("data"); 
      String token = data.optString("token"); 
     } else { 
      Log.e("message", "failure"); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
0

您可以使用Gsonjson轉換爲一個Java POJO類。不過,如果你只是想獲得令牌字段,你可以使用它像這樣:

public void onResponse(Response<Login> response, Retrofit retrofit) { 
     if(response.isSuccess()) { 
      try { 
     JSONObject ob = new JSONObject(response.body()); 
     String token = ob.getJSONObject("data").getString("token"); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
      Intent intent = new Intent(LoginActivity.this, ProfileActivity.class); 
      startActivity(intent); 
     } 
    }