How to get responce code and error message from retrofit 2.4.0 in Android
up vote
-2
down vote
favorite
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
add a comment |
up vote
-2
down vote
favorite
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
response.body().string() is also not working
– Yashwanth Reddy
Nov 20 at 7:25
response.code(), response.error().string() is also not working
– Yashwanth Reddy
Nov 20 at 8:07
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
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
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
javascript android android-studio retrofit retrofit2
edited Nov 20 at 7:20
Aniruddh Parihar
2,16111027
2,16111027
asked Nov 20 at 6:33
Yashwanth Reddy
22
22
response.body().string() is also not working
– Yashwanth Reddy
Nov 20 at 7:25
response.code(), response.error().string() is also not working
– Yashwanth Reddy
Nov 20 at 8:07
add a comment |
response.body().string() is also not working
– Yashwanth Reddy
Nov 20 at 7:25
response.code(), response.error().string() is also not working
– Yashwanth Reddy
Nov 20 at 8:07
response.body().string() is also not working
– Yashwanth Reddy
Nov 20 at 7:25
response.body().string() is also not working
– Yashwanth Reddy
Nov 20 at 7:25
response.code(), response.error().string() is also not working
– Yashwanth Reddy
Nov 20 at 8:07
response.code(), response.error().string() is also not working
– Yashwanth Reddy
Nov 20 at 8:07
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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
}
}
its not working
– Yashwanth Reddy
Nov 20 at 9:52
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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
}
}
its not working
– Yashwanth Reddy
Nov 20 at 9:52
add a comment |
up vote
0
down vote
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
}
}
its not working
– Yashwanth Reddy
Nov 20 at 9:52
add a comment |
up vote
0
down vote
up vote
0
down vote
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
}
}
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
}
}
answered Nov 20 at 6:47
Khemraj
10.9k22767
10.9k22767
its not working
– Yashwanth Reddy
Nov 20 at 9:52
add a comment |
its not working
– Yashwanth Reddy
Nov 20 at 9:52
its not working
– Yashwanth Reddy
Nov 20 at 9:52
its not working
– Yashwanth Reddy
Nov 20 at 9:52
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%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
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
response.body().string() is also not working
– Yashwanth Reddy
Nov 20 at 7:25
response.code(), response.error().string() is also not working
– Yashwanth Reddy
Nov 20 at 8:07