Android Retrofit | Post custom object (send json to server)

I was wondering how could I go about sending a custom object to my API using retrofit, something like this:

@POST(URL_ORDERS)
public void newOrder(Order order, Callback<Boolean> success);

Here's how I'd parse it on my server

public function store()
    {
    if(Auth::check()){
        $order = Input::get();
        $table = $order->table;
        $items = $order->items;

        if(!$table->taken){
            $table->taken = true;

            $order->push();
            $table->push();

            return true;
        }
    }

    return false;
}

For some reason I'm getting

06-04 20:45:59.275    6085-6306/com.tesis.restapp.restapp W/dalvikvm´╣Ľ threadid=11: thread exiting with uncaught exception (group=0x40aae210)
06-04 20:45:59.285    6085-6306/com.tesis.restapp.restapp E/AndroidRuntime´╣Ľ FATAL EXCEPTION: Retrofit-Idle
    java.lang.IllegalArgumentException: RestAppApiInterface.newOrder: No Retrofit annotation found. (parameter #1)
            at retrofit.RestMethodInfo.methodError(RestMethodInfo.java:120)
            at retrofit.RestMethodInfo.parameterError(RestMethodInfo.java:124)
            at retrofit.RestMethodInfo.parseParameters(RestMethodInfo.java:443)
            at retrofit.RestMethodInfo.init(RestMethodInfo.java:131)

I guess what I want it do to is to somehow transform my object into a json and send it to my server. Am I approaching this the right way?

Answers


The error is from not providing the @Body annotation on your Order parameter. Change it to:

@POST(URL_ORDERS)
public void newOrder(@Body Order order, Callback<Boolean> success);

Retrofit uses Gson to serialize and deserialize JSON by default. Gson uses variable names by default for serialization, but they can be changed using the annotation @SerializedName("replacement_name").

For Example, If your Order class looked like this:

public class Order {
    @SerializedName("custom_id")
    private int id;
    private String name;
    private List<Item> items;
}

public class Item {
    private int id;
    private String name;
}

Then Gson would automatically serialize that to

{
    "custom_id": 1,
    "name": "Hello Object",
    "items": [
        {
            "id": 1,
            "name": "Hello Item"
        }
    ]
}

Need Your Help

What is the algorithm identifier when using AES in CBC mode with ISO 10126 padding inside of PKCS#7?

encryption standards aes pkcs#7

I want to use PKCS#7 as a container format for some encrypted, signed content and we need to use AES in CBC mode with ISO 10126 based padding. I can't seem to find a concrete reference to an algori...

How can I guess action name from url in Rails?

ruby-on-rails routing

For example I have a url generated by something_path or something_url methods. I need to know action name from that url.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.