【问题标题】:POST method to send strings to JsonArrayPOST 方法将字符串发送到 JsonArray
【发布时间】:2020-01-15 04:42:04
【问题描述】:

我正在创建一个应用程序,我必须在其中向服务器发送或发布数据。我能够将数据发送到对象或数组,但我无法先在对象中发送数据,而不是在其子数组中发送数据,然后再在对象中发送数据。谁能帮我解决这个问题,我将非常感激。数据以下面给出的形式发送。

{
name:
no:
data: [
 {
    surname:
    option:
   another_option:
 }
]
another:
}

我想以字符串形式发布我的数据,因为它是对象形式 我想更新其中一个对象中的 ArrayList 但我无法获得如何在我的“数据:”中发送数据,因为我无法了解如何在我的参数中的数组列表中发布我的字符串值。可以有n个没有。我的 ArrayList 对象中的数组列表。我不知道如何在 POST 方法中发送或放入我的数据,请帮帮我

这是我正在尝试的代码。

 private void new_process() {

    String URL = "http://117.240.196.238:8080/api/raid";

    Log.i("response", URL);
    StringRequest jsonObjRequest = new StringRequest(
            Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("response_process", response);
            Upload_work(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("volley", "Error" + error.getMessage());
                    Log.i("response_error", error.toString());

                }
            }) {

        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded; charset=UTF-8";
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            String lic_date, lic_letter, lic_copy, sale_date, sale_letterno, sale_copy, material_name, material_quantity, quantity_in, fir_num, fir_dates, fir_policestation, sample_code, sample_date;                                       List<String> number = new ArrayList<>();

            number.add(first_text);
            number.add(second_text);
            number.add(third_text);

            params.put("name", name);
            params.put("no.", number);
            params.put("data", ?????); // how to pass my list of string number to this data
            params.put("another", another);

            return params;
        }

    };
    RequestQueue queue = SingletonRequestQueue.getInstance(getApplicationContext()).getRequestQueue();
    queue.add(jsonObjRequest);
}

private void Upload_work(String response) {
    Log.i("response_Upload_work", response);
    try {
        JSONObject json = new JSONObject(response);
        int success = json.getInt("success");
        String msg = json.getString("message");
        if (success == 1) {
            JSONObject c = json.getJSONObject("data");
            String messg = c.getString("message");
            Toast.makeText(this, messg, Toast.LENGTH_SHORT).show();


        } else {
            Toast.makeText(ScrollingActivity.this, msg, Toast.LENGTH_LONG).show();
        }
    } catch (Exception ex) {
        Log.i("responseNew m", "Exception : " + ex.toString());
    }

}

我尝试创建一个 JsonArray,然后创建一个 Jsonobject,然后在我的参数中传递 json 数组,但我无法将 jsonArray 与 jsonobject 链接起来

JsonArray sample_obj = new JsonArray();
                try {
                    JSONObject j = new JSONObject();

                    JSONObject c = new JSONObject(j.toString());

                    c.put("name", sample_code);
                    c.put("value", "google.com");

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

如果我以正确的方式做这件事或者我还需要做什么,请帮助我。

【问题讨论】:

    标签: android-studio android-volley


    【解决方案1】:
     JSONArray jsonArray = new JSONArray();
    
    for(**:**:**){
        JSONObject jsOb = new JSONObject();
        jsOb.put("surname", a);
        jsOb.put("option", b);
        jsOb.put("another_option", c);
        jsonArray.put(jsOb);
    }
    
     jQueue = Volley.newRequestQueue(getApplicationContext());
    
                String jurl = URL---------------------;
    
                final UserLocalStore userLocalStore = new UserLocalStore(getApplicationContext());
    
    
                JSONObject jsonObject= new JSONObject();
                try {
    
                    jsonObject.put("name", x);
                    jsonObject.put("no", y);
    
                    jsonObject.put("data", jsonArray);
                    jsonObject.put("another", z);
                }catch (JSONException e){
                    e.printStackTrace();
                }
    
    
    
    
    
                //Log.d("check param",jsonObject.toString());
    
                final JsonObjectRequest jrequest = new JsonObjectRequest(jurl, jsonObject,
                        new Response.Listener<JSONObject>() {
    
                            @Override
                            public void onResponse(JSONObject response) {
    
    
    
                                Log.d("Volley",response.toString());
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
    
                        Log.e("**VolleyError", "error" + error.getMessage());
    
                    }
                }){
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        return super.getParams();
                    }
    
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
    
    
                        Map<String, String> headers = new HashMap<>();
                        CurrentUser user = userLocalStore.getLoggedInUser();
    
    
                        String credentials = user.getUsername()+":"+user.getPassword();
                        String auth = "Basic "
                                + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                        headers.put("Content-Type", "application/json");
                        headers.put("Authorization", auth);
                        return headers;
                    }
                };
    
    
                jQueue.add(jrequest);
            }
    
    
    

    见上例

    【讨论】:

    • 更新了我的代码这将工作 JSONObject json2 = new JSONObject();尝试 { json2.put("name", name); json2.put("no", number); } catch (JSONException e) { e.printStackTrace(); } JsonArray sample_obj = new JsonArray(); sample_obj.add(String.valueOf(json2)); params.put("Raid_Sample", sample_obj.toString());
    • 如果我能够解决我的问题,那么我为什么要问问题。我只是想清除我的疑虑,如果我想先在 Object 中发送我的数据,然后在 Array 中发送我的数据,然后在 object 作为第一段的示例代码中的给定。
    • 你能解释一下你的疑问吗?我听不懂你说什么?
    • 先生,我只想使用 volley 将数据发布到服务器,但我无法获得如何以我在第一个编码部分中显示的确切形式发送数据,即在对象中发送数据和然后在有两个对象的数组中发送数据
    • 先生可以帮我解决如果我必须将数组列表中的多个对象发送到服务器我只得到一个列表数据或一个对象数据但我想将所有数据发布到服务器或每个对象到服务器
    【解决方案2】:

    你应该使用 JSONArray

    导入 org.json.JSONArray;

    不是 JsonArray

    导入 com.google.gson.JsonArray;

            JSONObject json2 = new JSONObject();
            JSONArray sample_obj = new JSONArray();
            try {
                json2.put("name", "name"); json2.put("no", "number");
                sample_obj.put(json2);
            } catch (JSONException e)
            { e.printStackTrace();
            }
    

    【讨论】:

    • JSONObject json2 = new JSONObject();尝试 { json2.put("name", name); json2.put("no", number); } catch (JSONException e) { e.printStackTrace(); } JsonArray sample_obj = new JsonArray(); sample_obj.add(String.valueOf(json2)); params.put("Raid_Sample", sample_obj.toString());
    • 先生,如果我必须在数组列表中发送多个对象怎么办,它只发送我的一个数据,如果我必须发送多个数据/对象怎么办
    猜你喜欢
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多