Reputation: 1314
I have used the below code (taken from SO) to post some data to a PHP script:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Web.API_PREFIX_GENERAL + "ajax/process.AL.php");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("mail", new StringBody("[email protected]"));
reqEntity.addPart("remote", new StringBody("1"));
reqEntity.addPart("altitle", new StringBody("GHI"));
reqEntity.addPart("aldesc", new StringBody("JKL"));
reqEntity.addPart("t", new StringBody("N"));
reqEntity.addPart("lat", new StringBody(lati));
reqEntity.addPart("long", new StringBody(longi));
reqEntity.addPart("p", new StringBody("all"));
httpPost.setEntity(reqEntity);
httpClient.execute(httpPost);
...And this does not produce anything. No error, but nothing gets posted either.
However, if I use something like below and have no parts added to reqEntity
,
String url = Web.API_PREFIX_GENERAL + "ajax/[email protected]&remote=1&altitle=GHI" +
"&aldesc=JKL&t=N&lat=" + lati + "&long=" + longi + "&p=all";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
httpPost.setEntity(reqEntity);
httpClient.execute(httpPost);
...this works
I have all the required Apache libraries set up also. So any idea why this is?
I need to use MultipartEntity
because I'll also have to post an image later.
Thanks.
Upvotes: 3
Views: 2005
Reputation: 2654
I think the problem isn't in the client side but in the server side. In the second example you are sending an HTTP POST but you are sending parameters like a GET.
Usually a Multipart request is used when you send data file (i.e. upload a file), but it seems to me in your code you aren't sending any data file, so you shouldn't use a multipart request.
Upvotes: 1
Reputation: 2662
You may have a problem with the integer to string conversion. Try this:
reqEntity.addPart("lat", new StringBody(Integer.toString(lati)));
reqEntity.addPart("long", new StringBody(Integer.toString(longi)));
You can use lati + ""
instead of Integer.toString(lati)
.
Upvotes: 0
Reputation: 336
I tested your code and monitor the http request using a http proxy tool. It works. Maybe some error occur in your php code.
class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
testUpload();
return null;
}
}
public void testUpload() {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.google.com/" + "ajax/process.AL.php");
try {
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("mail", new StringBody("[email protected]"));
reqEntity.addPart("remote", new StringBody("1"));
reqEntity.addPart("altitle", new StringBody("GHI"));
reqEntity.addPart("aldesc", new StringBody("JKL"));
reqEntity.addPart("t", new StringBody("N"));
reqEntity.addPart("p", new StringBody("all"));
httpPost.setEntity(reqEntity);
httpClient.execute(httpPost);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Upvotes: 0