Android Retrofit |发布自定义对象(将json发送到服务器)

时间:2022-08-26 17:01:36

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

我想知道如何使用改造将自定义对象发送到我的API,如下所示:

@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?

我想我想做的是以某种方式将我的对象转换为json并将其发送到我的服务器。我是以正确的方式接近这个吗?

1 个解决方案

#1


21  

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

错误来自于不在Order参数上提供@Body注释。将其更改为:

@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").

Retrofit默认使用Gson序列化和反序列化JSON。默认情况下,Gson使用变量名进行序列化,但可以使用注释@SerializedName(“replacement_name”)更改它们。

For Example, If your Order class looked like this:

例如,如果您的Order类看起来像这样:

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

然后Gson会自动将其序列化为

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

#1


21  

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

错误来自于不在Order参数上提供@Body注释。将其更改为:

@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").

Retrofit默认使用Gson序列化和反序列化JSON。默认情况下,Gson使用变量名进行序列化,但可以使用注释@SerializedName(“replacement_name”)更改它们。

For Example, If your Order class looked like this:

例如,如果您的Order类看起来像这样:

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

然后Gson会自动将其序列化为

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