what is the standard way to post JSON Object along with file using MultipartEntityBuilder and HTTP Client in Android what is the standard way to post JSON Object along with file using MultipartEntityBuilder and HTTP Client in Android json json

what is the standard way to post JSON Object along with file using MultipartEntityBuilder and HTTP Client in Android


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]",                            "rajesh123458@gmail.com");                    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                    }


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


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());