0

I am creating an app in which I have to send or Post data to the server. I am able to send data To object or array but I am not able to send data first in the object than in its subarray and then an object Can anyone help me out with this problem I will be very thankful. Data to be sent in the form given Below.

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

I want to POST my data in strings, as it is in object Form I want to update ArrayList in one of the objects but I am not able to get how to send data in My " data: " as I am not able to get how to post my string values in array list in my parameters. There can be n no. of array list in my objects of ArrayList. I am not able to get how to send or put my data in POST method please help me out

Here is my code I am trying.

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

}

I have tried to create a JsonArray and then create a Jsonobject and then pass the json array in my parameters but i am not able to link both jsonArray With 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();
                }

Please help me out if I am doing it in a correct way or what else I have to do.

2 Answers 2

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


see above example

Sign up to request clarification or add additional context in comments.

7 Comments

Updated my code Will this Work JSONObject json2 = new JSONObject(); try { 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());
if i am able to solve my problem then why i am asking Questions. I just want to clear my doubts that if i want to send my data in Object first then send my data in Array and then in object as givien in the sample code in first Paragraph.
Can you explain your doubt I can't understand what you said?
sir i just want to post data to server Using volley but i am not able to get how to send data in the exact form i have showing in first coding part that is to send data in a object and then send data in array which has two objects
sir can uh help me out what if i have to send multiple objects in array list to server i am getting only one list data or one object data but i want to post all data to server or every object to server
|
0

You should use JSONArray

import org.json.JSONArray;

not JsonArray

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

2 Comments

JSONObject json2 = new JSONObject(); try { 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());
sir what if i have to send multiple objects in array list its only sending my one data only what if i have to send multiple data/object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.