改造后方法给出空响应

时间:2020-03-29 16:43:04

标签: android api retrofit retrofit2

我想发布List<Airport>并使用Retrofit将其保存到Web服务器数据库。例如,我编写了一个简单的API,该API从Retrofit中获取列表,并响应与输入相同的列表,但是它提供了NULL值,如在日志中所见

*仅用于测试目的,以检查输入和输出是否相同。

Android代码:

 val call: Call<ResponseBody> = ApiClient.getClient.pushListAirport(
                            localListDiff, "Bearer "+Util.token)
                        call.enqueue(object : Callback<ResponseBody> {

                            override fun onResponse(call: Call<ResponseBody>?, response: Response<ResponseBody>?) {
                                Log.d("sfsdfsdf", Gson().toJson(response?.body()))
                                Toast.makeText(applicationContext, response?.body()?.name,Toast.LENGTH_LONG).show()
                            }

                            override fun onFailure(call: Call<ResponseBody>?, t: Throwable?) {
                                Toast.makeText(applicationContext, t?.message,Toast.LENGTH_LONG).show()
                            }

                        }) 

localListDiff示例:

[Airport(name=Name, created_at=2020-03-29 07:22:15, updated_at=2020-03-29 07:22:15, is_synced=0, id=9), Airport(name=df, created_at=2020-03-28 20:44:33, updated_at=2020-03-28 20:44:33, is_synced=1, id=10), Airport(name=Name, created_at=2020-03-29 07:39:40, updated_at=2020-03-29 07:39:40, is_synced=0, id=11)]

改造界面:

@FormUrlEncoded
    @POST("airports")
    fun pushListAirport(
        @Field("list[]") list : List<Airport>,
        @Header("Authorization") authHeader:String
    ): Call<ResponseBody>

日志:

2020-03-29 21:36:08.688 21729-21729/zohaiblab.devtros.kotlinpractice D/sfsdfsdf: null

Laravel API:

public function store($req) {
        return $req->input("list");
    }

public function store(Request $request)
    {
        try {
            $data =  $this->service->store($request);
            return response()->json($data, 201);
        } catch (Exception $e) {
            return response()->json(['message' => $e->getMessage()], 500);
        }
    }

0 个答案:

没有答案