16

This is a simple way of posting files from Android.

String url = "http://yourserver.com/upload.php"; File file = new File("myfileuri"); try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1); reqEntity.setContentType("binary/octet-stream"); reqEntity.setChunked(true); // Send in multiple parts if needed httppost.setEntity(reqEntity); HttpResponse response = httpclient.execute(httppost); //Do something with response... } catch (Exception e) { e.printStackTrace(); } 

What I want to do is add more POST variables to my request. How do I do that? While uploading plain strings in POST request, we use URLEncodedFormEntity.

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

Whereas while uploading files, we use InputStreamEntity.

Also, how do I specifically upload this file to $_FILES['myfilename']?

4
  • try using asynctask..you can use a hashmap if you want to upload different types of variables Commented Apr 12, 2013 at 9:53
  • obviously I will use asynctask. and yes usually hashmap of name value pairs is used for sending multiple variables. But, the entity types for String and File are different. That confuses me. Commented Apr 12, 2013 at 9:54
  • 1
    use multipart request Commented Apr 12, 2013 at 9:56
  • making multipart is not the doubt. i want to send normal strings along with files to the same php. Commented Apr 12, 2013 at 9:57

4 Answers 4

1

the most effective method is to use android-async-http

You can use this code to upload file :

 File myFile = new File("/path/to/file.png"); RequestParams params = new RequestParams(); try { params.put("profile_picture", myFile); } catch(FileNotFoundException e) {} 
Sign up to request clarification or add additional context in comments.

3 Comments

can I upload any kind of files using this Library and not have to specify mi e type
Yes. YOu can upload any kind of files
My advice is: stay away from loopj. I've used it for way too long, and it sucks in supporting SSL. It's been a known bug for a long time and apparently it can't been fixed due to the infrastructure used in the library. Use volley or retrofit/simplehttp instead.
1

After spending a full day found loopj. You can follow this code sample:

//context: Activity context, Property: Custom class, replace with your pojo public void postProperty(Context context,Property property){ // Creates a Async client. AsyncHttpClient client = new AsyncHttpClient(); //New File File files = new File(property.getImageUrl()); RequestParams params = new RequestParams(); try { //"photos" is Name of the field to identify file on server params.put("photos", files); } catch (FileNotFoundException e) { //TODO: Handle error e.printStackTrace(); } //TODO: Reaming body with id "property". prepareJson converts property class to Json string. Replace this with with your own method params.put("property",prepareJson(property)); client.post(context, baseURL+"upload", params, new AsyncHttpResponseHandler() { @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { System.out.print("Failed.."); } @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { System.out.print("Success.."); } }); } 

Comments

0

A few ways for you:

  • make 2 post requests: first with image File. Server returns an image id, and with second request you attach to this id yours params.

  • or, instead of the '2 request' solution, you could use MultipartEntity request. Look here for more datails

2 Comments

I wouldn't recommend the first suggestion. If you make two requests and either one of those fails, it can spell disaster for your data.
yeah, i do not recomend too, but there is such solution. I used the second one, it's much better
0

Since you want to upload files from your app, here is a good tutorial for you:

Uploading files to HTTP server using POST on Android.

If you want to upload strings as well, I think you already know the solution :)

2 Comments

hey Akash! thanks for your answer but it still did not answer my question. If you did not get my doubt, this question is exactly the same. stackoverflow.com/questions/11598457/… unfortunately this is unsolved too :(
okay i got it here are some questions with answers for your type of problem:<stackoverflow.com/questions/8949504/…> <stackoverflow.com/questions/8965022/…>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.