Https post request using curl : Android Https post request using curl : Android curl curl

Https post request using curl : Android


    BufferedReader in = null;    StringBuffer sb = new StringBuffer();    BufferedReader inPost = null;    try {        DefaultHttpClient httpclient = new DefaultHttpClient();            HttpPost httpost = new HttpPost(mURL);            httpost.setHeader("Accept","*/*");            httpost.setHeader("Content-Type", "application/x-www-form-urlencoded");            List <NameValuePair> nvps = new ArrayList<NameValuePair>();            nvps.add(new BasicNameValuePair("testkey1", "myvalue1"));            nvps.add(new BasicNameValuePair("testkey2", "myvalue2"));            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));            HttpResponse response = httpclient.execute(httpost);            HttpEntity entity = response.getEntity();            inPost = new BufferedReader(new InputStreamReader(entity.getContent()));            String linePost = "";            String NLPOST = System.getProperty("line.separator");            while ((linePost = inPost.readLine()) != null) {                sb.append(linePost + NLPOST);            }            inPost.close();            if (entity != null) {                entity.consumeContent();            }        httpclient.getConnectionManager().shutdown();   

also more on this link..http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/


After some hard time I got solution for my problem. Now, using MultipartEntity, I am able to send data to server like below.

    HttpClient httpClient = getHttpClient();    HttpPost httpost = new HttpPost("https://sample.com/api/posts");    MultipartEntity mpEntity = new MultipartEntity();    ContentBody cbFile1 = new FileBody(new File("file.png"), "image/png");    mpEntity.addPart("image", cbFile1);    ContentBody cbFile2 = new FileBody(new File("file.svg"), "image/svg+xml");    mpEntity.addPart("svgz", cbFile2);    ContentBody cbFile3 = new StringBody(getJsonData().toString(), "application/json", Charset.forName("UTF-8"));    mpEntity.addPart("json", cbFile3);    httpost.setEntity(mpEntity);