I am trying to do an HTTP Post to a URL.
I followed the code from this answer:
http://stackoverflow.com/questions/2935946/sending-images-using-http-post
All HttpComponent libraries has been successfully included to my project.
However i am getting this error when testing the code on the phone:
01-09 10:27:53.773: E/AndroidRuntime(4473): FATAL EXCEPTION: main
01-09 10:27:53.773: E/AndroidRuntime(4473): java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
01-09 10:27:53.773: E/AndroidRuntime(4473): at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:85)
01-09 10:27:53.773: E/AndroidRuntime(4473): at com.gelliesmedia.thumbqoo.services.HttpContentPost.sendPost(HttpContentPost.java:36)
01-09 10:27:53.773: E/AndroidRuntime(4473): at com.gelliesmedia.thumbqoo.ProductPublishActivity$4.onClick(ProductPublishActivity.java:224)
01-09 10:27:53.773: E/AndroidRuntime(4473): at android.view.View.performClick(View.java:4204)
01-09 10:27:53.773: E/AndroidRuntime(4473): at android.view.View$PerformClick.run(View.java:17355)
01-09 10:27:53.773: E/AndroidRuntime(4473): at android.os.Handler.handleCallback(Handler.java:725)
01-09 10:27:53.773: E/AndroidRuntime(4473): at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 10:27:53.773: E/AndroidRuntime(4473): at android.os.Looper.loop(Looper.java:137)
01-09 10:27:53.773: E/AndroidRuntime(4473): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-09 10:27:53.773: E/AndroidRuntime(4473): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 10:27:53.773: E/AndroidRuntime(4473): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 10:27:53.773: E/AndroidRuntime(4473): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-09 10:27:53.773: E/AndroidRuntime(4473): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-09 10:27:53.773: E/AndroidRuntime(4473): at dalvik.system.NativeStart.main(Native Method)
This is my Code:
@SuppressWarnings("deprecation")
public class HttpContentPost {
private static final String BASE_URL = "http://example.com/api/index.php";
public static void sendPost(String imagePath, ProductRaw data)
throws IOException, ClientProtocolException {
String responseBody;
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(BASE_URL);
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(imagePath);
ContentBody encFile = new FileBody(file,"image/jpg");
Log.i("TAG", "BUILDING HTTP ENTITY");
entity.addPart("file", encFile);
entity.addPart("action", new StringBody("publishMarket"));
entity.addPart("email", new StringBody(data.ownerEmail));
entity.addPart("password", new StringBody(data.ownerPasswd));
entity.addPart("title", new StringBody(data.productName));
entity.addPart("description", new StringBody(data.productDesc));
entity.addPart("currency_code", new StringBody(data.productCurrency));
entity.addPart("price", new StringBody(data.productPrice));
entity.addPart("category", new StringBody(data.productCat));
entity.addPart("tag", new StringBody(data.productTag));
request.setEntity(entity);
Log.i("TAG", request.toString());
ResponseHandler<String> responsehandler = new BasicResponseHandler();
responseBody = client.execute(request, responsehandler);
if (responseBody != null && responseBody.length() > 0) {
Log.w("TAG", "Response image upload" + responseBody);
}
}
}
Does anyone know how can i solve this?