Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.gameloop.planbox.Utils;
- import android.content.Context;
- import com.android.volley.AuthFailureError;
- import com.android.volley.DefaultRetryPolicy;
- import com.android.volley.NetworkResponse;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.ServerError;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.HttpHeaderParser;
- import com.android.volley.toolbox.StringRequest;
- import com.android.volley.toolbox.Volley;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.UnsupportedEncodingException;
- import java.util.HashMap;
- import java.util.Map;
- import br.com.gameloop.planbox.classes.ResponseResult;
- public class NetworkUtils {
- public static int REQUEST_TIMEOUT = 10000;
- private RequestQueue requestQueue;
- private ResponseResult responseResult;
- public NetworkUtils(Context context) {
- requestQueue = Volley.newRequestQueue(context);
- }
- public void doPost(final String url, final Map<String,String> params, final Callback callback) {
- StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
- new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- ResponseResult responseResult;
- try {
- JSONObject jsonObject = new JSONObject(response);
- if (jsonObject != null) {
- String message = "Requisição realizada com sucesso.";
- if(jsonObject.has("message")) {
- message = jsonObject.getString("message");
- }
- responseResult = new ResponseResult(jsonObject, message, true );
- callback.onResult(responseResult);
- } else {
- responseResult = new ResponseResult(null, "Ocorreu um erro, por favor tente novamente (001).", false );
- callback.onResult(responseResult);
- }
- } catch (Exception e) {
- responseResult = new ResponseResult(null, "Ocorreu um erro, por favor tente novamente. (002)", false );
- callback.onResult(responseResult);
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- NetworkResponse response = error.networkResponse;
- if (error instanceof ServerError || error instanceof AuthFailureError && response != null) {
- try {
- String errorResponse = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8"));
- JSONObject jsonObject = new JSONObject(errorResponse);
- String message = "Ocorreu um erro, por favor tente novamente. (003)";
- if(jsonObject.has("message")) {
- message = jsonObject.getString("message");
- }
- responseResult = new ResponseResult(jsonObject, message, true );
- callback.onResult(responseResult);
- } catch (UnsupportedEncodingException e1) {
- responseResult = new ResponseResult(null, "Ocorreu um erro, por favor tente novamente (004).", false );
- callback.onResult(responseResult);
- } catch (JSONException e2) {
- responseResult = new ResponseResult(null, "Ocorreu um erro, por favor tente novamente (005).", false );
- callback.onResult(responseResult);
- }
- }
- }
- }) {
- @Override
- protected Map<String,String> getParams() {
- return params;
- }
- @Override
- public Map<String, String> getHeaders() throws AuthFailureError {
- Map<String,String> params = new HashMap<String, String>();
- params.put("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
- return params;
- }
- };
- stringRequest.setRetryPolicy(new DefaultRetryPolicy(REQUEST_TIMEOUT,
- DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
- DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
- requestQueue.add(stringRequest);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement