Search code examples
javaandroidretrofit2rx-java2

How to post data using Retrofit as raw JSON


I am trying to post data using retrofit2 in the from of raw JSON to the server but there is no response from the server. Progress bar keeps on loading but there is no response from the server.

My server is sending below JSON object as a success message:

{
"status": "Success",
"statusCode": "S001",
"loginResponse": {
    "authenticationStatus": false,
    "sessionid": null,
    "errorDescription": null,
    "predictionStatus": null,
    "predictionPercentage": null,
    "predictionPercentageA": null,
    "predictionPercentageB": null,
    "predictionPercentageC": null
 }
}

I want to check for only status whether it's Success or not so I made below POJO class LoginResponse for that.

LoginResponse

public class LoginResponse {

@SerializedName("status")
String status;

public LoginResponse(){

}

public LoginResponse(String status) {
    this.status = status;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}
}

Below is my code:

ApiService.class

public interface ApiService {

@POST("userLoginVerification")
Call<LoginResponse> getResponse(@Body JsonObject body);
}

MainActivity.class

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    login = findViewById(R.id.login);
    email = findViewById(R.id.email);
    pwd = findViewById(R.id.pwd);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final ProgressDialog prg = new ProgressDialog(MainActivity.this);
            prg.setCancelable(false);
            prg.setMessage("Logging in...");
            prg.show();

            String str1 = email.getText().toString();
            String str2 = pwd.getText().toString();

            if(str1.equals("")){

                prg.dismiss();
                TastyToast.makeText(getApplicationContext(),"Enter email",TastyToast.LENGTH_SHORT,
                        TastyToast.ERROR).show();
            }
            else if(str2.equals("")){

                prg.dismiss();
                TastyToast.makeText(getApplicationContext(),"Enter password",TastyToast.LENGTH_SHORT,
                        TastyToast.ERROR).show();
            }
            else{

                Retrofit retrofit = RetrofitClient.getInstance();
                ApiService apiService = retrofit.create(ApiService.class);

                JsonObject jsonObject= new JsonObject();
                jsonObject.addProperty("userId",str1);
                jsonObject.addProperty("password",str2);

                Call<LoginResponse> call = apiService.getResponse(jsonObject);

                call.enqueue(new Callback<LoginResponse>() {
                    @Override
                    public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {

                       try{
                            JSONObject jsonObject1 = new JSONObject(resp);

                            String str = jsonObject1.getString("status");

                            if(str.equals("Success")){

                                prg.dismiss();
                                email.setText("");
                                pwd.setText("");
                            }
                           }
                          catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<LoginResponse> call, Throwable t) {

                        TastyToast.makeText(getApplicationContext(),t.getMessage(),TastyToast.LENGTH_SHORT,
                                   TastyToast.ERROR).show();
                    }
                });

            }
        }
    });
  }

There is no response from the server on implementing above code. What I am doing wrong?


Solution

  • public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response)

    Don't create JSONObject in onResponse. response.body() gives you LoginResponse object. Use that object to get status. Dismiss your progress bar immediately after onResponse or onFailure if activity or fragment is not destroyed.