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.