There is a string
variable and an image photo
taken from the camera
intent. The directory
location of the photo
is known. I want to make a HTTP post
of the string
variable and the image
photo to a webserver at the same time. Is that possible ? If so , how to do it ?

- 18,213
- 29
- 88
- 158
-
1Try `UrlEncoder.encode()`. – Ammar Apr 22 '15 at 09:43
-
1I think you have to just encode url parameters value only using UrlEncoder.encode() instead encode whole url. – Haresh Chhelana Apr 22 '15 at 09:46
2 Answers
From what I understand, you need to send an image and a string to your webserver within a single POST request. Here's how you'd proceed.
You first need to Base64 encode your image.
Start by converting your image into a byte array:
InputStream image = new FileInputStream(<path_to_image>);
byte[] buff = new byte[8192];
int readBytes;
ByteArrayOutputStream byteArrOS = new ByteArrayOutputStream();
try {
while ( (readBytes = inputStream.read(buff) ) != -1) {
byteArrOS.write(buff, 0, readBytes);
}
} catch (IOException e) {
e.printStackTrace();
}
byte[] b = byteArrOS.toByteArray();
Then convert it to Base64:
String bsfEncodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Then build a query with the the string and the resulting Base64 both encoded with URLEncoder and "utf-8":
strImgQuery = "str="+URLEncoder.encode(<string_data>, "utf-8")+"&image="+URLEncoder.encode(bsfEncodedImage, "utf-8");
Declare a new URL
:
URL postUrl = new URL("http://<IP>/postreq");
Open the connection:
HttpURLConnection conn = (HttpURLConnection)postUrl.openConnection();
Set output to "true" (needed for a POST request but not for GET):
conn.setDoOutput(true);
Set the request method to POST:
conn.setRequestMethod("POST");
The timeout:
conn.setReadTimeout(1e4);
Buffer the output to the output stream and flush/run:
Writer buffWriter = new OutputStreamWriter(conn.getOutputStream());
buffWrite.write(strImgQuery);
buffWriter.flush();
buffWriter.close();
At server side you'll get the str
and image
POST params which is dependent on your server implementation.
Note that your url must follow the URL Specification, otherwise you'll get a MalformedURLException
. If that's the case, be sure to check what exactly the issue is. For example if you use a non-existing ttp
"protocol" instead of http
your exception will look something like this:
java.net.MalformedURLException: unknown protocol: ttp
at java.net.URL.<init>(URL.java:592)
at java.net.URL.<init>(URL.java:482)
at java.net.URL.<init>(URL.java:431)
at com.pheromix.core.lang.NumberFormatExceptionExample.MalformedURLExceptionExample.sendGetRequest(MalformedURLExceptionExample.java:28)
at com.pheromix.core.lang.NumberFormatExceptionExample.MalformedURLExceptionExample.main(MalformedURLExceptionExample.java:17)
Also, this is a synchronous operation and is ran on the UI thread. It might be costly or it might not depending on other operations you're already running and the size of the POST data. If the problem arises, run the job on another thread.

- 1
- 1

- 1,220
- 11
- 24
-
I want also to post a string data which is the primary key associated to the photo ! – pheromix Apr 26 '15 at 15:51
-
for the information : I cannot post another question and I edited an older one because the website blocked me because I already posted 50 questions during one month :) – pheromix Apr 26 '15 at 16:08
-
@pheromix You are close to beating a record :) Anyway, check the edited answer. The previous one was really intended as a GET method, not a POST one. Are you able to get POST params in your server? – Alper Turan Apr 26 '15 at 16:20
-
I use PHP for the page which will be the target of the POST , so I think I can get POST params in the server :) – pheromix Apr 27 '15 at 05:04
-
1the app doesn't bear dealing with the compression , I searched another post on SO and I found a way using InputStream `http://stackoverflow.com/questions/4830711/how-to-convert-a-image-into-base64-string` : it is the Chandra Sekhar's answer :) – pheromix Apr 28 '15 at 07:22
You can use URLEncoder
String strUrl = "http://192.168.1.9/impots/" +URLEncoder.encode("outil.php?action=OutilImporterDonneesMobile", "utf-8");
URL url = new URL(strUrl);

- 1,062
- 8
- 19
-
-
-
-
@pheromix You need to just encode the params.. see the edited answer – Manish Kumar Apr 22 '15 at 13:29
-
it also raised the exception ! please delete your answer because the root of my problem is not the url but request send and reading issue ! – pheromix Apr 22 '15 at 15:51
-
@pheromix If you show the full exception it will be of help to see what's wrong. – Alper Turan Apr 26 '15 at 15:17