Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to post JSON ARRAY data to server in Android

I want to send below JSON data to server and read the response in android. Below is the Json data.

{
    "class": "OrderItemListDto",
    "orderItemList": [
        {
            "class": "OrderItemDto",
            "orderId": 24,
            "itemId": 1,
            "quantity": 2,
            "status": "NEW",
            "sRequest": "none"
        },
        {
            "class": "OrderItemDto",
            "orderId": 24,
            "itemId": 2,
            "quantity": 2,
            "status": "NEW",
            "sRequest": "none"
        }
    ]
}

Here May be data will be increased.

like image 753
Faiz Anwar Avatar asked Mar 10 '14 19:03

Faiz Anwar


People also ask

How to get data from JSON Object in android?

The method getJSONObject returns the JSON object. The method getString returns the string value of the specified key. This method returns the number of name/value mappings in this object.. This method returns an array containing the string names in this object.

How to get JSON Object from JSON Array android?

JSONArray objects have a function getJSONObject(int index) , you can loop through all of the JSONObjects by writing a simple for-loop: JSONArray array; for(int n = 0; n < array. length(); n++) { JSONObject object = array. getJSONObject(n); // do some stuff.... }

Can JSON body be an Array?

Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.


2 Answers

Check this code

JSONArray json = //your array;
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://your_url");

try {

    StringEntity se = new StringEntity(json.toString());

    httpPost.setEntity(se);
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");


    HttpResponse response = httpClient.execute(httpPost, httpContext); //execute your request and parse response
    HttpEntity entity = response.getEntity();

    String jsonString = EntityUtils.toString(entity); //if response in JSON format

} catch (Exception e) {
    e.printStackTrace();
}
like image 108
Sergey Pekar Avatar answered Oct 17 '22 00:10

Sergey Pekar


Android doesn't have special code for sending and receiving HTTP, you can use standard Java code. I'd recommend using the Apache HTTP client, which comes with Android. Here's a snippet of code I used to send an HTTP POST.

try {   int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpClient client =new DefaultHttpClient(httpParams);   
        HttpPost request =new HttpPost("");
        request.setEntity(new ByteArrayEntity(postMessage.toString().getBytes("UTF8")));
        HttpResponse response = client.execute(request);
        }catch (Exception e) {        
        }
like image 26
Vivek Kumar Samele Avatar answered Oct 17 '22 00:10

Vivek Kumar Samele