How to send JSON data as Body using Retrofit android

前端 未结 3 1716
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 02:38

I am trying to post below JSON array on server.

{
    \"order\": [{
            \"orderid\": \"39\",
            \"dishid\": \"54\",
            \"quantity\"         


        
3条回答
  •  日久生厌
    2021-01-06 03:33

    try to modify your code to Retrofit 2

    compile 'com.squareup.retrofit2:retrofit:2.0.0'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0'
    

    Your service :

    @POST("updateorder.php")
    Call updateorder(@Body JsonObject object);
    

    Call retrofit

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(RetrofitService.baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    

    Pass your Json using GSON :

    JsonObject postParam = new JsonObject
    postParam.addProperty("order",yourArray) ;
    

    Finally :

    Call call = retrofitService.updateorder(postParam);
    
    
        call.enqueue(new Callback() {
             @Override
             public void onResponse(Callcallback,Responseresponse) {
                String res = response.body();
             }
                @Override
                public void onFailure(Call call, Throwable t) {
    
                }
        });
    

    I hope to be helpful for you .

提交回复
热议问题