I am using Retrofit for the first time. So don't mind the silliness please. I have 2 problems:
Problem 1. How to get a value for the key "userId" from the API JSON response. (When isSuccessful is true) Following is the response from the API.
I am able to get the userId from the following messy code, kindly show me a better way:
JSONObject jsonObject = new JSONObject(new Gson().toJson(response.body()));
JSONArray userInfoObject = jsonObject.getJSONArray("userInfo");
JSONObject userIDObject = userInfoObject.getJSONObject(0);
Long userId = userIDObject.getLong("userId");
Problem 2: onResponse when isSuccessful is false, the response.body is appearing null. So how do i access the API JSON response in this case. However on using the Okhttp logging i find the okhttp logs the response in this case as well but the Retrofit callback shows response.body() as null.
Interface Class
public interface UserInfoClient {
@Headers("x-st-diagnostics-callerid: DKAPP-RETROFIT")
@POST("/api/User/")
Call<UserInfoModel> createUser(
@Header("x-st-diagnostics-correlationid") String uniqueID,
@Body UserInfoModel userInfo);
}
DataModel
public class UserInfoModel {
private List userInfo;
private String statusMessage;
private Integer statusCode;
private Long userId;
private String userFullName, userPassword, userName, userEmail, userMobileNumber, userWebsite, userDateOfBirth, userProfileText, userProfileDisplayPhoto, userProfileCoverPhoto;
public UserInfoModel(Long userId, String userFullName, String userPassword, String userName, String userEmail, String userMobileNumber, String userWebsite, String userDateOfBirth, String userProfileText, String userProfileDisplayPhoto, String userProfileCoverPhoto) {
this.userId = userId;
this.userFullName = userFullName;
this.userPassword = userPassword;
this.userName = userName;
this.userEmail = userEmail;
this.userMobileNumber = userMobileNumber;
this.userWebsite = userWebsite;
this.userDateOfBirth = userDateOfBirth;
this.userProfileText = userProfileText;
this.userProfileDisplayPhoto = userProfileDisplayPhoto;
this.userProfileCoverPhoto = userProfileCoverPhoto;
}
public Long getUserId() {
return userId;
}
public List getUserInfo() {
return userInfo;
}
public Integer getStatusCode() {
return statusCode;
}
public String getStatusMessage() {
return statusMessage;
}
}
Activity.java
public class ActivityUserInfo extends AppCompatActivity {
private static final String TAG = "ActivityUserInfo";
private EditText etUserId, etFullName, etPassword, etUsername, etEmail, etMobileNumber, etWebsite, etDob, etProfileTxt, etDispPhoto, etCoverPhoto;
private Button btnUpload;
public static Retrofit retrofit;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: begins");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_info);
etUserId = findViewById(R.id.et_userID);
etFullName = findViewById(R.id.et_fullName);
etPassword = findViewById(R.id.et_password);
etUsername = findViewById(R.id.et_username);
etEmail = findViewById(R.id.et_email);
etMobileNumber = findViewById(R.id.et_mobileNumber);
etWebsite = findViewById(R.id.et_website);
etDob = findViewById(R.id.et_dob);
etProfileTxt = findViewById(R.id.et_profileBio);
etDispPhoto = findViewById(R.id.et_profilePhoto);
etCoverPhoto = findViewById(R.id.et_coverPhoto);
btnUpload = findViewById(R.id.btn_uploadUserInfo);
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UserInfoModel userInfo = new UserInfoModel(
Long.parseLong(etUserId.getText().toString()),
etFullName.getText().toString(),
etPassword.getText().toString(),
etUsername.getText().toString(),
etEmail.getText().toString(),
etMobileNumber.getText().toString(),
etWebsite.getText().toString(),
etDob.getText().toString(),
etProfileTxt.getText().toString(),
etDispPhoto.getText().toString(),
etCoverPhoto.getText().toString()
);
sendNetworkRequest(userInfo);
}
});
}
private void sendNetworkRequest(UserInfoModel userInfo){
Log.d(TAG, "sendNetworkRequest: begins");
//Create okhttp client
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
//Adding logging interceptor tot he okHttp client
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//set logging interceptor properties
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
//To redact a header from logging
logging.redactHeader("Content-Type");
//Disable logging interceptor in mode other than DEBUG.
if (BuildConfig.DEBUG) {
//Adding logging interceptor to the okHttpClient using okHttp client builder (conditional for debug only)
okHttpClientBuilder.addInterceptor(logging);
}
//Create Retrofit Instance
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://abc.ett.io")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClientBuilder.build());
Retrofit retrofit = builder.build();
// Get client and call object for the request
UserInfoClient client = retrofit.create(UserInfoClient.class);
String uniqueID = UUID.randomUUID().toString(); //UUID for the header
Call<UserInfoModel> call = client.createUser(uniqueID, userInfo);
call.enqueue(new Callback<UserInfoModel>() {
@Override
public void onResponse(Call<UserInfoModel> call, Response<UserInfoModel> response) {
Log.d(TAG, "1. onResponse: body: " + response.body() + "\n message: " + response.message()
+ "\n code: " + response.code() + "\n headers: " + response.headers() + " errorBody: " + response.errorBody()
+ "\n isSuccessful: " + response.isSuccessful() + "\n response: " + response);
if (response.isSuccessful()) {
Toast.makeText(ActivityUserInfo.this, "Upload Successful.", Toast.LENGTH_SHORT).show();
try {
JSONObject jsonObject = new JSONObject(new Gson().toJson(response.body()));
JSONArray userInfoObject = jsonObject.getJSONArray("userInfo");
JSONObject userIDObject = userInfoObject.getJSONObject(0);
Long userId = userIDObject.getLong("userId");
Log.d(TAG, "onResponse: 1.5: " + userId);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(TAG, "2. onResponse: 200: Upload Successful."
+ " statusCode: " + response.body().getStatusCode()
+ " statusMessage: " + response.body().getStatusMessage()
+ " user ID: " + response.body().getUserId()
+ "\n userInfo: " + response.body().getUserInfo().get(0));
}
else{
if(response.body() != null){
APIError apiError = ErrorUtils.parseError(response);
Toast.makeText(ActivityUserInfo.this, "3. Save not successful", Toast.LENGTH_SHORT).show();
Log.d(TAG, "3. onResponse: Save not successful"
+" ErrorUtils httpStatusCode: " + apiError.getHttpStatusCode()
+" ErrorUtils customCode: " + apiError.getCustomCode()
+" ErrorUtils message: " + apiError.getMessage());
}
else{
Log.d(TAG, "4. onResponse: response.body() is null");
}
}
}
@Override
public void onFailure(Call<UserInfoModel> call, Throwable t) {
Log.d(TAG, "4. onFailure: t: " + t + " message: " + t.getMessage() + " toString: " + t.toString()
+ " getCause: " + t.getCause() + " getStackTrace: " + t.getStackTrace());
Toast.makeText(ActivityUserInfo.this, "Please check you internet connection.", Toast.LENGTH_SHORT).show();
}
});
}
}
Thanks for the help!