5

I am able to post json object using StringEntity using

httppost.setEntity(newStringEntity(obj.toString())); HttpResponse response = httpclient.execute(httppost);

but I have to post a file along with json data , I have found many Answers Using MultipartEntity but it has been deprecated , Please suggest me any tutorial or sample code which uses MultipartEntityBuilder , thanks
Here is my Code

Thread t = new Thread(new Runnable() { @Override public void run() { JSONObject jsonobject = null; String requestURL = "URL"; try { jsonobject = new JSONObject("{\"first\": \"Test\",\"last\": \"User\",\"name\": \"Test User\",\"email\": \"[email protected]\",\"birthdate\": \"1984-01-01\",\"Account\": {\"username\": \"t1r0123425899\",\"password\": \"testuser1234567899\"},\"Address\": {\"postal_code\": \"11230\",\"state_or_province\": \"NY\",\"country\": \"US\"}}"); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(requestURL); httppost.addHeader("Content-Type", "application/json"); httppost.addHeader("X-ConsumerApiClient","0f8f0a024d6344e429f5ee96aa66fbfb5c3973b5"); httppost.addHeader("X-ConsumerApiSignature", "qmt8aEAGRQUvldkDnHw8zgn1kRYuXRDwvmo3TpWlCtE="); MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create(); multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); multipartEntity.addPart("file", new FileBody(new File(fileName))); multipartEntity.addPart("Person", jsonobject.toString()); httppost.setEntity(multipartEntity.build()); try { HttpResponse response = httpclient.execute(httppost); Log.d("status", "" + response.getStatusLine()); Log.d("response",EntityUtils.toString(response.getEntity())); } catch (ClientProtocolException e) { } catch (IOException e) { } } }); t.start(); 

3 Answers 3

4

I found the solution,we can split the json object like following and send as a key value

 multipartEntity.addPart("data[Asset][file]", new FileBody(file, ContentType.APPLICATION_OCTET_STREAM, "filename.png")); multipartEntity.addTextBody("data[Person][first]", "kk"); multipartEntity.addTextBody("data[Person][email]", "[email protected]"); multipartEntity.addTextBody("data[Person][birthdate]", "1984-01-01"); multipartEntity.addTextBody("data[Person][Account][username]", "savita123458"); multipartEntity.addTextBody("data[Person][Account][password]", "testuser12345678"); httppost.setEntity(multipartEntity.build()); try { HttpResponse response = httpclient.execute(httppost); Log.d("status", response.getStatusLine().toString()); Log.d("data", EntityUtils.toString(response.getEntity())); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); // TODO Auto-generated catch block } 
Sign up to request clarification or add additional context in comments.

Comments

0

I have done something similar using http://loopj.com/android-async-http/, which can be used synchronously in your thread

1 Comment

Thanks for your reply , but I was expecting something else
0

Try doing this.

multipartEntity.addBinaryBody("file", file, ContentType.create("application/octet-stream"), file.getName()); multipartEntity.addTextBody("json", jsonobject.toString(), ContentType.DEFAULT_BINARY); httppost.setEntity(multipartEntity.build()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.