2

I'm using google Volley library to handle my application networking.

My backend is an apache server with Laravel 4. When i'm trying to make a POST request using JsonObjectRequest I'm getting an empty $_POST array.

I was able to solve it using the method described here: Volley JsonObjectRequest Post request not working

but it seems like a solution for simple cases where the object is not very complicated. Also, i'd like to use Gson and a custom GsonRequest, but it gives the same problem.

Seems odd to me that php and especially Laravel don't know how to deal with JSON requests as this is a REST framework, am I missing something? Is there a better way to solve it?

Community
  • 1
  • 1
Tomer
  • 17,787
  • 15
  • 78
  • 137

3 Answers3

0

Well, apparently the solution is very simple, just needed to dig deeper into documentation :)

http://laravel.com/docs/requests

and specifically this NOTE:

Some JavaScript libraries such as Backbone may send input to the application as JSON. You may access this data via Input::get like normal.

Tomer
  • 17,787
  • 15
  • 78
  • 137
0

This is how I solved it. Below is the custom class I created extending class of volley. Parameters is provided as Map

public class CustomJsonRequest extends Request {

Map<String, String> params;       
private Response.Listener listener; 

public CustomJsonRequest(int requestMethod, String url, Map<String, String> params,
                      Response.Listener responseListener, Response.ErrorListener errorListener) {

    super(requestMethod, url, errorListener);
    this.params = params;
    this.listener = responseListener;
}

@Override
protected void deliverResponse(Object response) {
    listener.onResponse(response); 

}

@Override
public Map<String, String> getParams() throws AuthFailureError {
         return params;
}

@Override
protected Response 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));
    }
}

}

and you can provide pamas as

Map<String, String> params = new HashMap<String, String>();
  params.put("email", email.getText().toString());
  params.put("password", password.getText().toString());

using this you will be able to use GET or POST verbs in your api.

user98239820
  • 1,411
  • 2
  • 16
  • 30
  • this is only good for simple cases where the object is flat, but what if you have a complex data structure you want to send? – Tomer Jan 21 '14 at 20:33
  • I have come across such situation but i think that can be solved by using $_POST['userinfo']['email']. – user98239820 Jan 22 '14 at 04:25
0

use this $_REQUEST NOT $_POST :D

Muhamet Aljobairi
  • 1,627
  • 4
  • 19
  • 25