7

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.

4
  • 2
    Magic? If that isn't an option, please post what you have so far and exactly what you are stuck on. Commented Mar 10, 2014 at 19:11
  • please use search first. For example stackoverflow.com/questions/7316501/… Commented Mar 10, 2014 at 19:11
  • Thanks, Now I send JSONObject data to server successfully because my data was like {"class":"OrderItemDto","orderId":24,"itemId":1}.But now I have to send multiple data that i.e JSONArray data like above at the question.So how I will send data and get response. Commented Mar 11, 2014 at 4:54
  • How about searching out and trying out yourself first? Commented Mar 11, 2014 at 5:04

3 Answers 3

7

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();
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

You can also send Json in string form to server using WebClient class.

WebClient webClient; 
        //show progress dialog
        Uri uriImageUploadURL = new Uri ( "ServerStringUploadUri" );
        webClient = webClient ?? new WebClient ();
        webClient.Headers.Add ( "Content-Type" , "text/json" );
        webClient.UploadStringAsync ( uriImageUploadURL , "POST" , "JsonStringToUpload" );
        webClient.UploadStringCompleted += StringUploadCompleted;

Comments

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.