如何在android中将更新的JSON返回给服务器

时间:2017-08-02 05:46:52

标签: android json

我正在创建一个使用JSON的应用。我想在以下JSON更新后,将相同的JSON返回给服务器。

[{
            "u1": "abc",
            "u2": "xyz",
            "u3": "From",
            "u4": "0",

}, {
            "u1": "abcd",
            "u2": "wxyz",
            "u3": "to",
            "u4": "0",

}, {
            "u1": "abcde",
            "u2": "vwxyz",
            "u3": "other",
            "u4": "0",

}]

我正在使用volley lib在这样的服务器上发送数据..

 private void makeJsonObjDataLAS() {


    JSONObject jsonparam = new JSONObject();
        jsonparam.put("Empid", LAempid);
        jsonparam.put("UserType", usertype);
        jsonparam.put("FirstReq", 0);

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

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, Constants.KEY_URL, jsonparam,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                    try {
                        JSONObject jsonReq = response.getJSONObject("Response");                            
                        if (jsonReq.getString("Code").equals("200")) {
                            status  = jsonReq.getString("Status");
                            message  = jsonReq.getString("Msg");
                            pDialog.dismiss();

                        }  

                    } catch (Exception e) {

                       pDialog.dismiss();                         

                    }

                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error){                              
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            return headers;
        }

    };
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    //jsonObjectRequest.setShouldCache(false);
    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
            Constants.TIME_OUT,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    requestQueue.add(jsonObjectRequest);
    // app.AppController.getInstance().addToRequestQueue(jsonObjReq,tag_json_obj);

}

我想更新(UserVal和UserTid)中的值并发送给服务器,请告诉我。

2 个答案:

答案 0 :(得分:0)

尝试这样做只需再次制作JSONObject并使用此类json.toString()方法将其发布到服务器

Button sendButton = (Button) findViewById(R.id.sendButton);

    sendButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            JSONObject postData = new JSONObject();
            try {

                // Convert your  object to JSON usin Gson like this
               JSONObject request;

               // convert your model class in to json object like this
               DataModel model= new DataModel();
               Gson gson = new Gson();
               String jsonString = gson.toJson(model);

                try {
                   request = new JSONObject(jsonString);

                } catch (JSONException e) {

                  // TODO Auto-generated catch block
                      e.printStackTrace();
                }
                // convert json object to string
                Gson gson = new Gson();
                String json = gson.toJson(request);
                // call here your service and pass parameter
                new SendDeviceDetails.execute("your service url", json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

答案 1 :(得分:0)

<强> 1。使用gson或其他支持的库将您的响应转换为相应的模型类。

List<Model> modelList = new Gson().fromJson(response,new TypeToken<List<Model>>);

2.根据您的要求更新值。

for(Model model : modelList ){
model.displayName = "your_values"; //just an example
}

3.将模型对象转换回json

String jsonBody = new Gson().toJson(model);

4. 将json发送到服务器。`

WebServiceRequest request = new WebServiceRequest(url,
Request.Method.POST,  new HashMap<String, String>(), jsonBody, this, this); // add listeners in your activity or fragment
RetryPolicy policy = new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
Volley.newRequestQueue(getApplicationContext()).add(request);