How to get error response from retrofit android












0















By This code, I am not able to get the error response as if I use same email id twice then I am getting the error response in postman but in my application, I am not getting the error response
So can you please help me to get out of this



My Interface



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


My Java Code



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);
}
}


My Activity



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();
}


Postman Error response










share|improve this question

























  • Try different email for every signup.

    – Harshil kakadiya
    Nov 22 '18 at 6:46











  • refer stackoverflow.com/questions/32519618/…

    – sasikumar
    Nov 22 '18 at 6:47
















0















By This code, I am not able to get the error response as if I use same email id twice then I am getting the error response in postman but in my application, I am not getting the error response
So can you please help me to get out of this



My Interface



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


My Java Code



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);
}
}


My Activity



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();
}


Postman Error response










share|improve this question

























  • Try different email for every signup.

    – Harshil kakadiya
    Nov 22 '18 at 6:46











  • refer stackoverflow.com/questions/32519618/…

    – sasikumar
    Nov 22 '18 at 6:47














0












0








0








By This code, I am not able to get the error response as if I use same email id twice then I am getting the error response in postman but in my application, I am not getting the error response
So can you please help me to get out of this



My Interface



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


My Java Code



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);
}
}


My Activity



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();
}


Postman Error response










share|improve this question
















By This code, I am not able to get the error response as if I use same email id twice then I am getting the error response in postman but in my application, I am not getting the error response
So can you please help me to get out of this



My Interface



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


My Java Code



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);
}
}


My Activity



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();
}


Postman Error response







java android retrofit2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 6:58









ADM

8,822102352




8,822102352










asked Nov 22 '18 at 6:44









Yashwanth ReddyYashwanth Reddy

11




11













  • Try different email for every signup.

    – Harshil kakadiya
    Nov 22 '18 at 6:46











  • refer stackoverflow.com/questions/32519618/…

    – sasikumar
    Nov 22 '18 at 6:47



















  • Try different email for every signup.

    – Harshil kakadiya
    Nov 22 '18 at 6:46











  • refer stackoverflow.com/questions/32519618/…

    – sasikumar
    Nov 22 '18 at 6:47

















Try different email for every signup.

– Harshil kakadiya
Nov 22 '18 at 6:46





Try different email for every signup.

– Harshil kakadiya
Nov 22 '18 at 6:46













refer stackoverflow.com/questions/32519618/…

– sasikumar
Nov 22 '18 at 6:47





refer stackoverflow.com/questions/32519618/…

– sasikumar
Nov 22 '18 at 6:47












1 Answer
1






active

oldest

votes


















0














make error pojo class..



public class Errors{

@SerializedName("email")
private List<String> email;

public void setEmail(List<String> email){
this.email = email;
}

public List<String> getEmail(){
return email;
}
}


make changes into response body class..



public class ResponseBody {

@SerializedName("errors")
private Errors errors;

public void setErrors(Errors errors){
this.errors = errors;
}

public Errors getErrors(){
return errors;
}


}



then after used into api response like



      Errors errors=response.body().getErrors();





share|improve this answer
























  • if i signup with different email id then it should show success response. is it possible through that. @Harsha

    – Yashwanth Reddy
    Nov 22 '18 at 7:19













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%2f53425243%2fhow-to-get-error-response-from-retrofit-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














make error pojo class..



public class Errors{

@SerializedName("email")
private List<String> email;

public void setEmail(List<String> email){
this.email = email;
}

public List<String> getEmail(){
return email;
}
}


make changes into response body class..



public class ResponseBody {

@SerializedName("errors")
private Errors errors;

public void setErrors(Errors errors){
this.errors = errors;
}

public Errors getErrors(){
return errors;
}


}



then after used into api response like



      Errors errors=response.body().getErrors();





share|improve this answer
























  • if i signup with different email id then it should show success response. is it possible through that. @Harsha

    – Yashwanth Reddy
    Nov 22 '18 at 7:19


















0














make error pojo class..



public class Errors{

@SerializedName("email")
private List<String> email;

public void setEmail(List<String> email){
this.email = email;
}

public List<String> getEmail(){
return email;
}
}


make changes into response body class..



public class ResponseBody {

@SerializedName("errors")
private Errors errors;

public void setErrors(Errors errors){
this.errors = errors;
}

public Errors getErrors(){
return errors;
}


}



then after used into api response like



      Errors errors=response.body().getErrors();





share|improve this answer
























  • if i signup with different email id then it should show success response. is it possible through that. @Harsha

    – Yashwanth Reddy
    Nov 22 '18 at 7:19
















0












0








0







make error pojo class..



public class Errors{

@SerializedName("email")
private List<String> email;

public void setEmail(List<String> email){
this.email = email;
}

public List<String> getEmail(){
return email;
}
}


make changes into response body class..



public class ResponseBody {

@SerializedName("errors")
private Errors errors;

public void setErrors(Errors errors){
this.errors = errors;
}

public Errors getErrors(){
return errors;
}


}



then after used into api response like



      Errors errors=response.body().getErrors();





share|improve this answer













make error pojo class..



public class Errors{

@SerializedName("email")
private List<String> email;

public void setEmail(List<String> email){
this.email = email;
}

public List<String> getEmail(){
return email;
}
}


make changes into response body class..



public class ResponseBody {

@SerializedName("errors")
private Errors errors;

public void setErrors(Errors errors){
this.errors = errors;
}

public Errors getErrors(){
return errors;
}


}



then after used into api response like



      Errors errors=response.body().getErrors();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 6:51









Android TeamAndroid Team

7,62211335




7,62211335













  • if i signup with different email id then it should show success response. is it possible through that. @Harsha

    – Yashwanth Reddy
    Nov 22 '18 at 7:19





















  • if i signup with different email id then it should show success response. is it possible through that. @Harsha

    – Yashwanth Reddy
    Nov 22 '18 at 7:19



















if i signup with different email id then it should show success response. is it possible through that. @Harsha

– Yashwanth Reddy
Nov 22 '18 at 7:19







if i signup with different email id then it should show success response. is it possible through that. @Harsha

– Yashwanth Reddy
Nov 22 '18 at 7:19






















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%2f53425243%2fhow-to-get-error-response-from-retrofit-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

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]