0
public class UploadImage extends Activity {
    InputStream inputStream;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new myclass().execute();
    }

    class myclass extends AsyncTask<Void, Void, Void>

    {

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.ic_launcher);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); // compress
                                                                    // to
            // which format
            // you want.
            byte[] byte_arr = stream.toByteArray();
            String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            try {
                String finalnamevalue;
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(
                        "http://api.lociiapp.com/api/registration/SaveProfilePicture");
                JSONObject contactsObj = new JSONObject();
                nameValuePairs.add(new BasicNameValuePair("member_id", "380"));
                nameValuePairs.add(new BasicNameValuePair("imageFile",
                        image_str));
                nameValuePairs.add(new BasicNameValuePair("picture_path",
                        "380.jpg"));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                String the_string_response = convertResponseToString(response);

            } catch (Exception e) {

                System.out.println("Error in http connection " + e.toString());
            }

            return null;
        }

        public String convertResponseToString(HttpResponse response)
                throws IllegalStateException, IOException {

            String res = "";
            StringBuffer buffer = new StringBuffer();
            inputStream = response.getEntity().getContent();
            int contentLength = (int) response.getEntity().getContentLength(); // getting
                                                                                // content
                                                                                // length…..

            if (contentLength < 0) {
            } else {
                byte[] data = new byte[512];
                int len = 0;
                try {
                    while (-1 != (len = inputStream.read(data))) {
                        buffer.append(new String(data, 0, len)); // converting
                                                                    // to
                                                                    // string
                                                                    // and
                                                                    // appending
                                                                    // to
                                                                    // stringbuffer…..
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    inputStream.close(); // closing the stream…..
                } catch (IOException e) {
                    e.printStackTrace();
                }
                res = buffer.toString(); // converting stringbuffer to string…..

                // System.out.println("Response => " +
                // EntityUtils.toString(response.getEntity()));
            }
            return res;
        }

    }
}

This is My code for Upload image to server i am able to send parameter to server in name value pair [member_id=380, imageFile=bytestream,picturepathe="380.jpg"] . while i have to Post parameter like this {[imageFile:"bystram",member_id:"380",picture_path:"380.jpg"]}

please help how i will make Name value pair in Json format

Edge
  • 925
  • 5
  • 15
  • 31
  • You want to send JSON data or Form Encoded Entities which you are using currently? – Tejas Aug 06 '14 at 06:52
  • yes {[imageFile:"bystram",member_id:"380",picture_path:"380.jpg"]} like this – Edge Aug 06 '14 at 06:54
  • currntly i am posting like this [imageFile:"bystram",member_id:"380",picture_path:"380.jpg"] – Edge Aug 06 '14 at 06:55
  • your server side code in java or .net? because in server side they should get (3)each parameter seperately in current case. if you are moving to json, then they can get all these in single and they have to parse that parameter – manivannan Aug 06 '14 at 06:58
  • Have you tried JSONObject class ? – Tejas Aug 06 '14 at 06:58
  • please tell how i will make it in Json , i have .net server – Edge Aug 06 '14 at 07:00
  • You'll find how to post parameters in JSON in this link http://stackoverflow.com/questions/3027066/how-to-send-a-json-object-over-request-with-android – Tejas Aug 06 '14 at 07:04

2 Answers2

0

you can post json object like the following

 class myclass extends AsyncTask<Void, Void, Void>

{

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); // compress
                                                                // to
        // which format
        // you want.
        byte[] byte_arr = stream.toByteArray();
        String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);

        try {
            String finalnamevalue;
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://api.lociiapp.com/api/registration/SaveProfilePicture");
            JSONObject contactsObj = new JSONObject();
                contactsObj.put("member_id", "380");
                contactsObj.put("imageFile", image_str);
                contactsObj.put("picture_path", "380.jpg");
             StringEntity stringEntity = new StringEntity( contactsObj.toString());  
                stringEntity.setContentType("application/json;charset=UTF-8");
                stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                "application/json;charset=UTF-8"));
            httppost.setEntity(stringEntity);
            HttpResponse response = httpclient.execute(httppost);
            String the_string_response = convertResponseToString(response);

        } catch (Exception e) {

            System.out.println("Error in http connection " + e.toString());
        }

        return null;
    }

change the post header and content types.

manivannan
  • 622
  • 1
  • 4
  • 17
  • its goes one by one parameter and something hidden when i debug it – Edge Aug 06 '14 at 07:26
  • what do you mean by hidden? – manivannan Aug 06 '14 at 07:31
  • Hidden means when i debug then Bytstream at end ...... like this and member id added with json object added one by One – Edge Aug 06 '14 at 07:37
  • yes in debug it will display like that.because of it's(image byte[] string) heap size will be larger. so that in debug it cant display to the user after reaching it's max size. but the data will be there. – manivannan Aug 06 '14 at 07:47
0

You can post data in json like this way:-

String json = "";
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("id", "");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                jsonObject.put("MSISDN", number);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

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

            HttpResponse httpResponse = null;
Anjali
  • 206
  • 3
  • 10