How to post json object using retrofit

瘦欲@ 提交于 2019-12-25 06:35:38

问题


i wanted to post the json object using retrofit. i have created following interface:

public interface syncinter {

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

}

the following data i want it to post.

 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());

I have written the following retrofit code to post the json object but with this following code I am getting

failure -> error : 400 Bad Request.

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:


i was able to post it using TypedInput declare the api as shown below

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

and send the json object like this

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) {               
            }
        });

not of relative importance i am using retrofit 1.9



来源:https://stackoverflow.com/questions/37267975/how-to-post-json-object-using-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!