Pinki
Pinki

Reputation: 21929

How Can I Send A JSON Array as Req to URL

I'm using the below code for sending the Jsonarray objetc to url. But, it is not working.

HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost("urlname");        
JSONObject json = new JSONObject();
        JSONArray Text_Field_Array = new JSONArray();
        JSONObject json_Text_Field = new JSONObject();
        json_Text_Field.put("pagenum", "1");
        json_Text_Field.put("text", "text field 1");
        json_Text_Field.put("font", "Arial");
        json_Text_Field.put("fontSize", "18");
        json_Text_Field.put("color", "#ff0000");
        json_Text_Field.put("x", "100");
        json_Text_Field.put("y", "0");
        json_Text_Field.put("alignment", "left");
        Text_Field_Array.put(json_Text_Field);
        JSONArray image_Field_Array = new JSONArray();
        JSONObject json_image_Field = new JSONObject();
        json_image_Field.put("image", "http://clintons-test.redboxdigital.net/media/uploads/llll.png");
        json_image_Field.put("pagenum", "1");
        json_image_Field.put("scale", "1");
        json_image_Field.put("rotation", "90");
        json_image_Field.put("x", "100");
        json_image_Field.put("y", "10");
        json_image_Field.put("width", "100");
        json_image_Field.put("height", "100");
        image_Field_Array.put(json_image_Field);
        json.put("images", image_Field_Array);
        json.put("all_text_fields", Text_Field_Array);
        json.put("sku", "CCI1022537");

StringEntity se = new StringEntity( "JSON: " + json.toString());  
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

        httppost.setEntity(se);


        Log.v("URI","==="+ httppost);

        HttpResponse response = httpclient.execute(httppost);
        InputStream is = response.getEntity().getContent();
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();


        sb = new StringBuilder();
        byte[] buffer = new byte[512];
        int bytes_read = 0;

        while((bytes_read = is.read(buffer, 0, buffer.length)) > 0)
        {
            sb.append( new String(buffer,0,bytes_read));
        }

I used the above code by considering the primalpop's answer in sending Json as req to url I think the problem is:

StringEntity se = new StringEntity( "JSON: " + json.toString());  
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

            httppost.setEntity(se);

Any one have idea on how we send the Json array to url, give me some suggestions. Thanks in advance

Upvotes: 0

Views: 2838

Answers (1)

Pinki
Pinki

Reputation: 21929

JSONObject json=new JSONObject();

         JSONArray Text_Field_Array = new JSONArray();

        JSONObject json_Text_Field = new JSONObject();
        json_Text_Field.put("pagenum", "1");
        json_Text_Field.put("text", "text field 1");
        json_Text_Field.put("font", "Arial");
        json_Text_Field.put("fontSize", "18");
        json_Text_Field.put("color", "ff0000");
        json_Text_Field.put("x", "100");
        json_Text_Field.put("y", "0");
        json_Text_Field.put("alignment", "left");

        Text_Field_Array.put(json_Text_Field);


        JSONArray image_Field_Array = new JSONArray();
        JSONObject json_image_Field = new JSONObject();
        json_image_Field.put("image", "http://clintons-test.redboxdigital.net/media/uploads/llll.png");
        json_image_Field.put("pagenum", "1");
        json_image_Field.put("scale", "1");
        json_image_Field.put("rotation", "90");
        json_image_Field.put("x", "100");
        json_image_Field.put("y", "10");
        json_image_Field.put("width", "100");
        json_image_Field.put("height", "100");

        image_Field_Array.put(json_image_Field);

        json.put("images", image_Field_Array);
        json.put("all_text_fields", Text_Field_Array);
        json.put("sku", "CCI1022537");


List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("carddata", json.toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

By using above code we can send the json formatted string as req in android.

Upvotes: 1

Related Questions