如何使用retrofit2在密钥中发布多个值

时间:2017-01-20 08:24:51

标签: android retrofit2

我在服务器端创建API,当我尝试使用POSTMAN这样发布时..

tokenku:2040asdfasdfasdfCBLCPTDPaS99P7L0N9LYP8X4sdfasdf
    userdetail:2
    invoiceNo:(AUTO)
    invoiceDate:20/01/2017
    customer:6
    uangMuka:0.00
    description:keterangan aja, wkwkwkwwk
    item:38
    qty:10
    unit:1
    price:10000
    discount:0.00
    dt:P
    totalberat:100
    province:DKi Jakarta
    city:DKI Jakarta
    alamatTujuan:Jl. Villa Kapuk Mas 1 Blok F2 No.10
    ppn:null
    item:40
    qty:10
    unit:1
    price:10000
    discount:0.00
    dt:P
    ongkir:18500

enter image description here

仔细查看,我发送了两次密钥itemqtyunitpricediscountdt

enter image description here

我尝试使用POSTMAN,并在server收到该内容时成功。

现在我正尝试用retrofit2

实现该值

这就是我的尝试。

  @FormUrlEncoded
@POST("api/orderPenjualan/save")
Call<String> postOrder(@Field("invoiceNo") String invoiceNo,
                       @Field("invoiceDate") String invoiceDate,
                       @Field("customer") String userId,
                       @Field("ppn") Object nullValue, //how to post null value
                       @Field("uangMuka") String cashAdvance,
                       @Field("description") String description,
                       //loop parameter if more than one item
                       @Field("item") int itemId,
                       @Field("qty") double qty,
                       @Field("unit") int unit,
                       @Field("price") double price,
                       @Field("discount") double discount,
                       @Field("dt") String point,
                       @Field("userdetail") String userdetailConstant,
                       @Field("tokenku") String tokenkuConstant);

@Multipart
@POST("api/orderPenjualan/save")
Call<String> postOrderWithBody(@Part MultipartBody.Part fields //body not working
);

@POST("api/orderPenjualan/save")
Call<String> postOrderWithMap(@Query("invoiceNo") String invoiceNo,
                              @Query("invoiceDate") String invoiceDate,
                              @Query("customer") int userId,
                              @Query("ppn") String nullValue, //how to post "null" value
                              @Query("uangMuka") double cashAdvance, //0.00
                              @Query("description") String description,
                              @Query("totalberat") int weight,
                              @Query("province") String province,
                              @Query("city") String city,
                              @Query("alamatTujuan") String destAddress,
                              @Query("ongkir") String ongkir,
                              //loop parameter if more than one item
                              @Body() RequestBody fields, //body not working
                              @Query("userdetail") String userdetailConstant,
                              @Query("tokenku") String tokenkuConstant);

当我尝试执行此代码时,服务器刚收到 item:38 qty:10 unit:1 price:10000 discount:0.00 dt:P

不是这个 item:40 qty:10 unit:1 price:10000 discount:0.00 dt:P

在我使用改装之前,我使用此代码来处理

 for(int i=0; i<mItemDataItem.size(); i++) {
                            HelperAddItem hai = mItemDataItem.get(i);

                            final String FmItemId = String.valueOf(hai.getmItemId());
                            final String FmItemQty = String.valueOf(hai.getmQty());
                            final String FmUnitId = String.valueOf(hai.getmUnitId());
                            final String FmPrice = String.valueOf(hai.getmPrice());
                            final String FmDiscount = String.valueOf(hai.getmDiscountNominal());
                            final String FmDT = (hai.getmDiscountType() == 1 ? "P" : "PS");

                            parameter.add(new BasicNameValuePair("item", String.valueOf(FmItemId)));
                            parameter.add(new BasicNameValuePair("qty", String.valueOf(FmItemQty)));
                            parameter.add(new BasicNameValuePair("unit", String.valueOf(FmUnitId)));
                            parameter.add(new BasicNameValuePair("price", String.valueOf(FmPrice)));
                            parameter.add(new BasicNameValuePair("discount", String.valueOf(FmDiscount)));
                            parameter.add(new BasicNameValuePair("dt", FmDT));
                        }

但是在改造2中怎么样?

这段代码对我有用...... 但我想使用retrofit2

AsyncTask postServer = new AsyncTask() {
protected String mResponse = "";
protected ProgressDialog dialog;

@Override
    protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(CheckOutActivity.this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.setMessage("Harap tunggu, Sedang menyimpan Order...");
dialog.show();
    }

@Override
    protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
        ArrayList parameter = new ArrayList();
        parameter.add(new BasicNameValuePair("invoiceNo", invoice));
        parameter.add(new BasicNameValuePair("invoiceDate", invoiceDate));
        parameter.add(new BasicNameValuePair("customer", String.valueOf(userId)));
        parameter.add(new BasicNameValuePair("ppn", "null"));
        parameter.add(new BasicNameValuePair("uangMuka", String.valueOf(uangMuka)));
        parameter.add(new BasicNameValuePair("description", String.valueOf(des)));
        parameter.add(new BasicNameValuePair("province", String.valueOf(city.getProvince())));
        parameter.add(new BasicNameValuePair("city", String.valueOf(city.getCity_name())));
        parameter.add(new BasicNameValuePair("alamatTujuan", String.valueOf(detailAddress)));
        parameter.add(new BasicNameValuePair("ongkir", String.valueOf(selectedOngkir)));
        parameter.add(new BasicNameValuePair("userdetail", Constants.USERDETAIL));
        parameter.add(new BasicNameValuePair("tokenku", Constants.TOKENKU));

for (int i = 0; i < orderList.size(); i++) {
            ProductOrderModel current = orderList.get(i);

            parameter.add(new BasicNameValuePair("item", String.valueOf(current.getItem())));
            parameter.add(new BasicNameValuePair("qty", String.valueOf(current.getQty())));
            parameter.add(new BasicNameValuePair("unit", String.valueOf(current.getUnit())));
            parameter.add(new BasicNameValuePair("price", String.valueOf(current.getPrice())));
            parameter.add(new BasicNameValuePair("discount", String.valueOf(current.getDiscount())));
            parameter.add(new BasicNameValuePair("dt", current.getDt()));
        }
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://192.168.0.106:8080/api/orderPenjualan/save");
try {
            post.setEntity(new UrlEncodedFormEntity(parameter, "UTF-8"));
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
return mResponse = EntityUtils.toString(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
return null;
    }

@Override
    protected void onPostExecute(String result) {
super.onPostExecute(result);
dialog.dismiss();
        Log.d(TAG, "onPostExecute: "+mResponse);
//Toast.makeText(mContext, result, Toast.LENGTH_SHORT).show();
    }
};
postServer.execute();

1 个答案:

答案 0 :(得分:0)

点击此链接https://futurestud.io/tutorials/retrofit-multiple-query-parameters-of-same-name,您就可以添加多个具有相同名称的查询参数