如何将JSON数据发送到Android中的服务器

时间:2018-02-25 05:03:53

标签: android json

我想将json数据发送到服务器,但我无法做到。我已经粘贴了以下json数据的有效载荷,请检查并帮助我。在我的Json Data有效负载中,这么多的数组和对象都会给我发送问题而发送服务器。我已经检查了谷歌中的所有帖子,但无法做到。

//payload

{
"action":"create",
"machinetypelist":[{"id":"","materialTypeId":"1","machineinplantid":"MIPID-103","material":["1","2"]}]
}

4 个答案:

答案 0 :(得分:0)

您可以使用Gson从模型中发送json数据。

答案 1 :(得分:0)

//escape the double quotes in json string
String payload="{\"action\":\"create\",\"machinetypelist\":[{\"id\":\"\",\"materialTypeId\":\"1\",\"machineinplantid\":\"MIPID-103\",\"material\":[\"1\",\"2\"]}]}"
String requestUrl="your url";
sendPostRequest(requestUrl, payload);

创建sendPostRequest方法。这会奏效。我提到了link

答案 2 :(得分:0)

This is the sollution for my question-
JSONObject js = new JSONObject ();
            try {
                js.put ("action","create");
                JSONObject jsonObject = new JSONObject ();
                jsonObject.put ("id","");
                jsonObject.put ("materialTypeId","");
                jsonObject.put ("machineinplantid","");
                JSONArray jsonArray = new JSONArray ();
                jsonArray.put ();
                jsonObject.put ("material",jsonArray);
                JSONArray jsonArray1 = new JSONArray ();
                jsonArray1.put (jsonObject);
                js.put ("machinetypelist",jsonArray1);

            } catch (JSONException e) {
                e.printStackTrace ( );
            }

答案 3 :(得分:0)

使用Volley,您可以按照以下要求发送JSON数据,我希望这有助于您了解如何发送JsonObjectRequest

private void sendJsonData(JSONObject jsonObjectRequest) {

    String url = "your_url";

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObjectRequest,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {

                    Log.d("TAG", "onResponse: get your response here ");
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.d("TAG", "onErrorResponse: ERROR");
        }
    }) {

    };
    AppController.getInstance().addToRequestQueue(jsonObjectRequest);

}