1

I'm using Volley as networking handler of my application. My back-end is Laravel 5 on apache server. I'm trying to POST using custom request as described here: Volley JsonObjectRequest Post request not working , but it keeps error with 500 code error response. Are there any other sollutions? or am i missing something? i'm not familiar with Laravel. Thanks.

here's my code:

public class CustomRequest extends Request<JSONObject> {

    private Listener<JSONObject> listener;
    private Map<String, String> params;

    public CustomRequest(String url, Map<String, String> params,
                         Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    public CustomRequest(int method, String url, Map<String, String> params,
                         Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    protected Map<String, String> getParams()
            throws com.android.volley.AuthFailureError {
        Log.d("parameter", params.toString());
        return params;
    };



    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        //headers.put("X-CSRF-Token", params.get("_token"));
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        //Log.d("token",params.get("_token"));
        return headers;
    }

    @Override
    protected void deliverResponse(JSONObject response) {
        // TODO Auto-generated method stub
        listener.onResponse(response);
    }
}

on main activity:

final String POST_URL = "https://www.pinus.com/api/registertoserver";
    final HashMap<String, String> postParams = new HashMap<String, String>();

    postParams.put("email", email);
    postParams.put("first_name", firstName);
    postParams.put("last_name", lastName);
    postParams.put("alamat", adress);
    postParams.put("notelp", phone);


    final RequestQueue queue = Volley.newRequestQueue(getApplicationContext(), new OkHttpStack());

    CustomRequest jsonObjReq = new CustomRequest(Request.Method.POST, POST_URL, postParams, new com.android.volley.Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {
                Log.d("result", response.toString());
                if (response.getBoolean("status")) {
                    //pDialog.dismiss();
                    finish();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    // Adding request to request queue
    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
            10000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(jsonObjReq);
    queue.start();
Community
  • 1
  • 1
klaudiusnaban
  • 196
  • 1
  • 10

1 Answers1

1

I know you asked 6 months ago, but I will answer, maybe it would be useful for someone else. I had same problem, and problem was CSRF token. In file : App\Http\Middleware\VerifyCsrfTokenMiddleware.php, find this

protected $except = [

    ];

and add route for your API.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
MilanBL
  • 21
  • 3