Sample app side code that worked for me. You can send image in one valuepair and the json in another valuepair: (Here "uploadedfile" tag defines the valuepair as the path of the image file in sd card during checking in post function, other tags will be considered as text data)
List<NameValuePair> values; values = new ArrayList<NameValuePair>();
System.out.println(Constants.all_datas.get(pos).getBookName());
values.add(new NameValuePair("uploadedfile",
Constants.book_image_path
+ Constants.all_datas.get(pos).getImage()));
values.add(new NameValuePair("id", Constants.all_datas.get(pos)
.getBookid() + ""));
values.add(new NameValuePair("bookname", Constants.all_datas
.get(pos).getBookName()));
values.add(new NameValuePair("price", Constants.all_datas.get(
pos).getPrice()));
values.add(new NameValuePair("writtername", Constants.all_datas
.get(pos).getWritterName()));
values.add(new NameValuePair("publishername",
Constants.all_datas.get(pos).getPublisherName()));
post(values);
// Post Function
public void post(final List<NameValuePair> nameValuePairs) {
// Setting progressDialog properties
progressDialog = ProgressDialog.show(CustomBookActivity.this, "",
"Syncing Book Data...");
mHandler = new Handler();
// Function to run after thread
mUpdateResults = new Runnable() {
public void run() {
progressDialog.dismiss();
// Something
}
};
new Thread() {
@Override
public void run() {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(URL);
try {
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
for (int index = 0; index < nameValuePairs.size(); index++) {
if (nameValuePairs.get(index).getName()
.equalsIgnoreCase("uploadedfile")) {
// If the key equals to "uploadedfile", we use FileBody
// to transfer the data
entity.addPart(
nameValuePairs.get(index).getName(),
new FileBody(new File(nameValuePairs.get(
index).getValue())));
} else {
// Normal string data
entity.addPart(nameValuePairs.get(index).getName(),
new StringBody(nameValuePairs.get(index)
.getValue()));
}
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
HttpEntity result_entity = response.getEntity();
String htmlResponse = EntityUtils.toString(result_entity);
result = htmlResponse;
System.out.println("SYNC:::" + result);
// server = true;
} catch (IOException e) {
e.printStackTrace();
// server = false;
}
// dismiss the progress dialog
// Calling post function
mHandler.post(mUpdateResults);
}
}.start();
}
Apache Mime4J, HTTPCore, HTTPMime libary jars need to be added to project