How to get responce code and error message from retrofit 2.4.0 in Android












-2















while signing up, I am unable to get the response code and error message so can you help me?



This is My Interface



public interface SignupAPI {
@FormUrlEncoded
@POST("users")
Call<ResponseBody> createUser(
@Field("email") String email,
@Field("password") String password,
@Field("role") String role
);
}


This is Java Class



public class SignupClient {
private static final String BASE_URL = "http://74.207.233.160/api/v1/";
private static SignupClient mInstance;
private Retrofit retrofit;
private SignupClient(){
retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
}
public static synchronized SignupClient getmInstance(){
if (mInstance == null){
mInstance = new SignupClient();
}
return mInstance;
}
public SignupAPI getApi(){
return retrofit.create(SignupAPI.class);
}
}


This Is Signup Activity



progressBar.setVisibility(View.VISIBLE);
Call<ResponseBody> call = SignupClient.getmInstance().getApi().createUser(email, password,role);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()){
progressBar.setVisibility(View.GONE);
Toast.makeText(SignupActivity.this, "Account Sucessfully Created", Toast.LENGTH_SHORT).show();
}else {
try {
progressBar.setVisibility(View.GONE);
JSONObject jsonError = new JSONObject(response.errorBody().string());
Toast.makeText(SignupActivity.this, jsonError.getString("errors"),Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
progressBar.setVisibility(View.GONE);
e.printStackTrace();
} catch (IOException e) {
progressBar.setVisibility(View.GONE);
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(SignupActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}


I am not able to get error message and also response code so please help me to get it.



Here is the Postman PostMan API










share|improve this question

























  • response.body().string() is also not working

    – Yashwanth Reddy
    Nov 20 '18 at 7:25











  • response.code(), response.error().string() is also not working

    – Yashwanth Reddy
    Nov 20 '18 at 8:07
















-2















while signing up, I am unable to get the response code and error message so can you help me?



This is My Interface



public interface SignupAPI {
@FormUrlEncoded
@POST("users")
Call<ResponseBody> createUser(
@Field("email") String email,
@Field("password") String password,
@Field("role") String role
);
}


This is Java Class



public class SignupClient {
private static final String BASE_URL = "http://74.207.233.160/api/v1/";
private static SignupClient mInstance;
private Retrofit retrofit;
private SignupClient(){
retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
}
public static synchronized SignupClient getmInstance(){
if (mInstance == null){
mInstance = new SignupClient();
}
return mInstance;
}
public SignupAPI getApi(){
return retrofit.create(SignupAPI.class);
}
}


This Is Signup Activity



progressBar.setVisibility(View.VISIBLE);
Call<ResponseBody> call = SignupClient.getmInstance().getApi().createUser(email, password,role);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()){
progressBar.setVisibility(View.GONE);
Toast.makeText(SignupActivity.this, "Account Sucessfully Created", Toast.LENGTH_SHORT).show();
}else {
try {
progressBar.setVisibility(View.GONE);
JSONObject jsonError = new JSONObject(response.errorBody().string());
Toast.makeText(SignupActivity.this, jsonError.getString("errors"),Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
progressBar.setVisibility(View.GONE);
e.printStackTrace();
} catch (IOException e) {
progressBar.setVisibility(View.GONE);
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(SignupActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}


I am not able to get error message and also response code so please help me to get it.



Here is the Postman PostMan API










share|improve this question

























  • response.body().string() is also not working

    – Yashwanth Reddy
    Nov 20 '18 at 7:25











  • response.code(), response.error().string() is also not working

    – Yashwanth Reddy
    Nov 20 '18 at 8:07














-2












-2








-2


1






while signing up, I am unable to get the response code and error message so can you help me?



This is My Interface



public interface SignupAPI {
@FormUrlEncoded
@POST("users")
Call<ResponseBody> createUser(
@Field("email") String email,
@Field("password") String password,
@Field("role") String role
);
}


This is Java Class



public class SignupClient {
private static final String BASE_URL = "http://74.207.233.160/api/v1/";
private static SignupClient mInstance;
private Retrofit retrofit;
private SignupClient(){
retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
}
public static synchronized SignupClient getmInstance(){
if (mInstance == null){
mInstance = new SignupClient();
}
return mInstance;
}
public SignupAPI getApi(){
return retrofit.create(SignupAPI.class);
}
}


This Is Signup Activity



progressBar.setVisibility(View.VISIBLE);
Call<ResponseBody> call = SignupClient.getmInstance().getApi().createUser(email, password,role);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()){
progressBar.setVisibility(View.GONE);
Toast.makeText(SignupActivity.this, "Account Sucessfully Created", Toast.LENGTH_SHORT).show();
}else {
try {
progressBar.setVisibility(View.GONE);
JSONObject jsonError = new JSONObject(response.errorBody().string());
Toast.makeText(SignupActivity.this, jsonError.getString("errors"),Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
progressBar.setVisibility(View.GONE);
e.printStackTrace();
} catch (IOException e) {
progressBar.setVisibility(View.GONE);
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(SignupActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}


I am not able to get error message and also response code so please help me to get it.



Here is the Postman PostMan API










share|improve this question
















while signing up, I am unable to get the response code and error message so can you help me?



This is My Interface



public interface SignupAPI {
@FormUrlEncoded
@POST("users")
Call<ResponseBody> createUser(
@Field("email") String email,
@Field("password") String password,
@Field("role") String role
);
}


This is Java Class



public class SignupClient {
private static final String BASE_URL = "http://74.207.233.160/api/v1/";
private static SignupClient mInstance;
private Retrofit retrofit;
private SignupClient(){
retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
}
public static synchronized SignupClient getmInstance(){
if (mInstance == null){
mInstance = new SignupClient();
}
return mInstance;
}
public SignupAPI getApi(){
return retrofit.create(SignupAPI.class);
}
}


This Is Signup Activity



progressBar.setVisibility(View.VISIBLE);
Call<ResponseBody> call = SignupClient.getmInstance().getApi().createUser(email, password,role);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()){
progressBar.setVisibility(View.GONE);
Toast.makeText(SignupActivity.this, "Account Sucessfully Created", Toast.LENGTH_SHORT).show();
}else {
try {
progressBar.setVisibility(View.GONE);
JSONObject jsonError = new JSONObject(response.errorBody().string());
Toast.makeText(SignupActivity.this, jsonError.getString("errors"),Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
progressBar.setVisibility(View.GONE);
e.printStackTrace();
} catch (IOException e) {
progressBar.setVisibility(View.GONE);
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(SignupActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}


I am not able to get error message and also response code so please help me to get it.



Here is the Postman PostMan API







javascript android android-studio retrofit retrofit2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 7:20









Aniruddh Parihar

2,18911027




2,18911027










asked Nov 20 '18 at 6:33









Yashwanth ReddyYashwanth Reddy

284




284













  • response.body().string() is also not working

    – Yashwanth Reddy
    Nov 20 '18 at 7:25











  • response.code(), response.error().string() is also not working

    – Yashwanth Reddy
    Nov 20 '18 at 8:07



















  • response.body().string() is also not working

    – Yashwanth Reddy
    Nov 20 '18 at 7:25











  • response.code(), response.error().string() is also not working

    – Yashwanth Reddy
    Nov 20 '18 at 8:07

















response.body().string() is also not working

– Yashwanth Reddy
Nov 20 '18 at 7:25





response.body().string() is also not working

– Yashwanth Reddy
Nov 20 '18 at 7:25













response.code(), response.error().string() is also not working

– Yashwanth Reddy
Nov 20 '18 at 8:07





response.code(), response.error().string() is also not working

– Yashwanth Reddy
Nov 20 '18 at 8:07












1 Answer
1






active

oldest

votes


















0














Here I have written how you can get HTTP code from both success response and error response.



@Override
public void onResponse(Call<T> call, Response<T> response) {
int statusCode = response.code();
}

@Override
public void onFailure(Call<T> call, Throwable t) {
if (new Exception(t) instanceof HttpException) {
int statusCode = ((HttpException) t).getStatusCode();
else {
// unknown error
}
}





share|improve this answer
























  • its not working

    – Yashwanth Reddy
    Nov 20 '18 at 9:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387481%2fhow-to-get-responce-code-and-error-message-from-retrofit-2-4-0-in-android%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Here I have written how you can get HTTP code from both success response and error response.



@Override
public void onResponse(Call<T> call, Response<T> response) {
int statusCode = response.code();
}

@Override
public void onFailure(Call<T> call, Throwable t) {
if (new Exception(t) instanceof HttpException) {
int statusCode = ((HttpException) t).getStatusCode();
else {
// unknown error
}
}





share|improve this answer
























  • its not working

    – Yashwanth Reddy
    Nov 20 '18 at 9:52
















0














Here I have written how you can get HTTP code from both success response and error response.



@Override
public void onResponse(Call<T> call, Response<T> response) {
int statusCode = response.code();
}

@Override
public void onFailure(Call<T> call, Throwable t) {
if (new Exception(t) instanceof HttpException) {
int statusCode = ((HttpException) t).getStatusCode();
else {
// unknown error
}
}





share|improve this answer
























  • its not working

    – Yashwanth Reddy
    Nov 20 '18 at 9:52














0












0








0







Here I have written how you can get HTTP code from both success response and error response.



@Override
public void onResponse(Call<T> call, Response<T> response) {
int statusCode = response.code();
}

@Override
public void onFailure(Call<T> call, Throwable t) {
if (new Exception(t) instanceof HttpException) {
int statusCode = ((HttpException) t).getStatusCode();
else {
// unknown error
}
}





share|improve this answer













Here I have written how you can get HTTP code from both success response and error response.



@Override
public void onResponse(Call<T> call, Response<T> response) {
int statusCode = response.code();
}

@Override
public void onFailure(Call<T> call, Throwable t) {
if (new Exception(t) instanceof HttpException) {
int statusCode = ((HttpException) t).getStatusCode();
else {
// unknown error
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 6:47









KhemrajKhemraj

13.6k43979




13.6k43979













  • its not working

    – Yashwanth Reddy
    Nov 20 '18 at 9:52



















  • its not working

    – Yashwanth Reddy
    Nov 20 '18 at 9:52

















its not working

– Yashwanth Reddy
Nov 20 '18 at 9:52





its not working

– Yashwanth Reddy
Nov 20 '18 at 9:52




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387481%2fhow-to-get-responce-code-and-error-message-from-retrofit-2-4-0-in-android%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents