Skip to content

Commit

Permalink
Fixed error handling and PUT request
Browse files Browse the repository at this point in the history
  • Loading branch information
F4pl0 committed Sep 7, 2019
1 parent e57450c commit 9803221
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
10 changes: 10 additions & 0 deletions farla/src/main/java/com/f4pl0/farla/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.f4pl0.farla;

public abstract class Constants {
public static int
NO_CONNECTION = 1,
AUTH_FAILURE = 2,
SERVER_ERROR = 3,
NETWORK_ERROR = 4,
PARSE_ERROR = 5;
}
18 changes: 17 additions & 1 deletion farla/src/main/java/com/f4pl0/farla/FarlaDeleteRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
Expand Down Expand Up @@ -49,7 +55,17 @@ public void onResponse(String response) {
{
@Override
public void onErrorResponse(VolleyError error) {

if (error instanceof TimeoutError || error instanceof NoConnectionError) {
listener.onFailure(Constants.NO_CONNECTION);
} else if (error instanceof AuthFailureError) {
listener.onFailure(Constants.AUTH_FAILURE);
} else if (error instanceof ServerError) {
listener.onFailure(Constants.SERVER_ERROR);
} else if (error instanceof NetworkError) {
listener.onFailure(Constants.NETWORK_ERROR);
} else if (error instanceof ParseError) {
listener.onFailure(Constants.PARSE_ERROR);
}
}
}
);
Expand Down
17 changes: 17 additions & 0 deletions farla/src/main/java/com/f4pl0/farla/FarlaGetRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
Expand Down Expand Up @@ -50,6 +56,17 @@ public void onResponse(String response) {
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
listener.onFailure(Constants.NO_CONNECTION);
} else if (error instanceof AuthFailureError) {
listener.onFailure(Constants.AUTH_FAILURE);
} else if (error instanceof ServerError) {
listener.onFailure(Constants.SERVER_ERROR);
} else if (error instanceof NetworkError) {
listener.onFailure(Constants.NETWORK_ERROR);
} else if (error instanceof ParseError) {
listener.onFailure(Constants.PARSE_ERROR);
}
}
});
requestQueue.add(stringRequest);
Expand Down
19 changes: 18 additions & 1 deletion farla/src/main/java/com/f4pl0/farla/FarlaPostRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
Expand Down Expand Up @@ -56,13 +62,24 @@ public void execute(){
{
@Override
public void onResponse(String response) {

listener.onSuccess(response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
listener.onFailure(Constants.NO_CONNECTION);
} else if (error instanceof AuthFailureError) {
listener.onFailure(Constants.AUTH_FAILURE);
} else if (error instanceof ServerError) {
listener.onFailure(Constants.SERVER_ERROR);
} else if (error instanceof NetworkError) {
listener.onFailure(Constants.NETWORK_ERROR);
} else if (error instanceof ParseError) {
listener.onFailure(Constants.PARSE_ERROR);
}
}
}
) {
Expand Down
17 changes: 17 additions & 0 deletions farla/src/main/java/com/f4pl0/farla/FarlaPutRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
Expand Down Expand Up @@ -62,6 +68,17 @@ public void onResponse(String response) {
{
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
listener.onFailure(Constants.NO_CONNECTION);
} else if (error instanceof AuthFailureError) {
listener.onFailure(Constants.AUTH_FAILURE);
} else if (error instanceof ServerError) {
listener.onFailure(Constants.SERVER_ERROR);
} else if (error instanceof NetworkError) {
listener.onFailure(Constants.NETWORK_ERROR);
} else if (error instanceof ParseError) {
listener.onFailure(Constants.PARSE_ERROR);
}
}
}
) {
Expand Down

0 comments on commit 9803221

Please sign in to comment.