如何使用改造发布json对象

时间:2016-05-17 05:38:52

标签: android json gson retrofit

我想使用改版发布json对象。 我创建了以下界面:

public interface syncinter {

@POST("url")
void sync_data(@Body JSONObject ordObj, Callback<JsonObject> callback);

}

我希望它发布以下数据。

 final JSONObject ordJsonObj = new JSONObject();
    JSONArray ordJsonArray = new JSONArray();
    try {
        ordJsonObj.put("Order_Nos",mOrdArr.size());

        for (int i=0; i<mOrdArr.size();i++) {

            JSONObject ordObj = new JSONObject();
            ordObj.put("Order_No",mOrdArr.get(i).getorderID());
            ordObj.put("Order_Date",mOrdArr.get(i).getorderDate());
            ordObj.put("Customer_id",mOrdArr.get(i).getCustPKID());

            Customer aCust = db.getEmployee(mOrdArr.get(i).getCustPKID());
            ordObj.put("Cust_name",aCust.getEmpName());// query DB
            ordObj.put("Company_id",sharedpreferences.getString(OrderApplication.CompanyID,"")); // sharedP
            ordObj.put("Device_Ref",mOrdArr.get(i).getOrdPKID());// sharedP
            ordObj.put("User_ID",sharedpreferences.getString(OrderApplication.UserID,""));// sharedP

            JSONArray prodJsonArray = new JSONArray();
            ArrayList<Product> mProdArr = db.getAllProductOrder(mOrdArr.get(i).getorderID());

            for (int j=0; j<mProdArr.size();j++) {

                JSONObject prodObj = new JSONObject();
                prodObj.put("Product_id",mProdArr.get(j).getPrID());
                prodObj.put("Product_name",mProdArr.get(j).getprName());
                prodObj.put("Product_Brand",mProdArr.get(j).getBrandName());
                prodObj.put("Qty",mProdArr.get(j).getPrQty());
                prodObj.put("Rate",(Double.parseDouble(mProdArr.get(j).getPrAmt()+"")/ mProdArr.get(j).getPrQty()));
                prodObj.put("Total_Amount",(Double.parseDouble(mProdArr.get(j).getPrAmt()+"")));
                prodJsonArray.put(j, prodObj);
            }
            ordObj.put("OrderDetails",prodJsonArray);

            ordJsonArray.put(i,ordObj);
        }
        ordJsonObj.put("Orders",ordJsonArray);

        Log.d("response", "" + ordJsonObj.toString());

我已经编写了以下改进代码来发布json对象但是我正在使用以下代码

  

失败 - &gt;错误:400错误请求。

RestAdapter adapter = new RestAdapter.Builder()
                    .setEndpoint(OrderApplication.getBase_URL)
                    .build();

            //Creating object for our interface
            syncinter api = adapter.create(syncinter.class);


            api.sync_data(ordJsonObj, new Callback<JsonObject>() {
                @Override
                public void success(JsonObject jsonObject, retrofit.client.Response response) {

                }



                @Override
                public void failure(RetrofitError error) {
                   Log.d("ERROR", String.valueOf(error));
                    Toast.makeText(getActivity(),"Order failed to place on Server",Toast.LENGTH_LONG).show();
                }
            });

1 个答案:

答案 0 :(得分:0)

我能够使用TypedInput发布它,如下所示声明api

 @PUT(ServerEndPoint)
    void callApi(@Body TypedInput input, Callback<Response> response);

并像这样发送json对象

TypedInput input = null;
        try {
            JSONObject obj = new JSONObject();
            obj.put("KEY", "value");
            input = new TypedByteArray("application/json", obj.toString().getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        callApi(input, new Callback<Response>() {
            @Override
            public void success(Response response, Response response2) {
            }

            @Override
            public void failure(RetrofitError error) {               
            }
        });

不是相对重要我正在使用改造1.9