0

Hi i have so much googled about image upload from android phone to server and i have done following code

for example

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("image")) { entity.addPart(nameValuePairs.get(index).getName(),new FileBody(new File( nameValuePairs.get( index) .getValue()), "image/jpg")); httpPost.setEntity(entity); HttpResponse httpResponse = httpClient.execute(httpPost, localContext); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); 

Don't know why but image is not uploded to server and i am getting following logcat

 05-08 23:20:17.452: I/System.out(2501): Response from serveer <pre>Array 05-08 23:20:17.454: I/System.out(2501): ( 05-08 23:20:17.455: I/System.out(2501): [image] => Array 05-08 23:20:17.455: I/System.out(2501): ( 05-08 23:20:17.455: I/System.out(2501): [name] => IMG_20130101_164850.jpg 05-08 23:20:17.455: I/System.out(2501): [type] => 05-08 23:20:17.455: I/System.out(2501): [tmp_name] => C:\Windows\Temp\php5CD4.tmp 05-08 23:20:17.456: I/System.out(2501): [error] => 0 05-08 23:20:17.456: I/System.out(2501): [size] => 1988151 05-08 23:20:17.456: I/System.out(2501): ) 05-08 23:20:17.456: I/System.out(2501): ) 

Here from server TYPE is returning no value so i can not give update

Please anybody can help about this problem

2 Answers 2

1

I have tested and it's fully working

You just need to change

This code to

MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE);

to

MultipartEntity entity = new MultipartEntity( HttpMultipartMode.STRICT);

and it will be done all other code is fine

Sign up to request clarification or add additional context in comments.

Comments

0

There are two ways that come to mind:

Bad (Eats up memory)

MultipartEntity entity = new MultipartEntity(); FileBody fileBody = new FileBody(...) entity.addPart("nameOfThePartTheServerExpects", fileBody); entity.addPart(...) // additional parts if the server needs stuff. httpPost.setEntity(entity); httpClient.execute(httpPost, localContext); 

Good (Better for memory)

MultipartEntity entity = new MultipartEntity(); InputStreamBody inputStreamBody = new InputStreamBody(is, someRandomFilename); entity.addPart("nameOfThePartTheServerExpects", inputStreamBody); entity.addPart(...) // additional parts if the server needs stuff. httpPost.setEntity(entity); httpClient.execute(httpPost, localContext); 

I would strongly suggest that you inspect the post from a web page for a normal web implementation so you can see what the names for all the Parts are, those Parts will correspond to all the entity.addPart().

Also your use of FileBody is deprecated, please see the API.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.