Thursday, September 29, 2011

send json object to a json request

Through Below code we can add the json object to json request in android

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(HTTPname);
        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();
       
       
        File f=new File("/sdcard/SelectImage.png");
        ContentBody cb=new FileBody(f,"image/png");
//        reqEntity.addPart("point_image", cb);
       
        json_image_Field.put("image", cb);
        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));
        HttpResponse response = httpclient.execute(httppost);
        InputStream is = response.getEntity().getContent();

        if(response!=null){ 
            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));
            }
            System.out.println("Preview image Result=="+sb.toString());
        }

No comments:

Post a Comment