How to get error response from retrofit android
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();
}
java android retrofit2
add a comment |
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();
}
java android retrofit2
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
add a comment |
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();
}
java android retrofit2
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();
}
java android retrofit2
java android retrofit2
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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();
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
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
add a comment |
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();
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
add a comment |
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();
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();
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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