Android 安卓改造| 发布自定义对象(将 json 发送到服务器)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24049434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:45:13  来源:igfitidea点击:

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

androidjsonpostlaravel

提问by FRR

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 并将其发送到我的服务器。我是否以正确的方式接近这个?

回答by James McCracken

The error is from not providing the @Bodyannotation on your Orderparameter. Change it to:

错误是由于未@Body在您的Order参数上提供注释。将其更改为:

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

Retrofit uses Gsonto 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 默认使用变量名进行序列化,但可以使用 annotation 更改它们@SerializedName("replacement_name")

For Example, If your Orderclass 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"
        }
    ]
}