2

I used to have a combination of a child of the Request object that uses a MultipartEnityBuilder and HttpEntity to upload images and files. Now that it is deprecated, and now that I have migrated to Android Studio, I am looking for a more updated way to do the above task but using the currently best way to do it that is compatible from API 15 onwards.

I tried searching but Google still returns the guides and questions here on SO that references MultipartEntityBuilder and HttpEntity, so I figured I could ask

John Ernest Guadalupe
  • 6,379
  • 11
  • 38
  • 71
  • 1
    Have you read http://stackoverflow.com/questions/32240177/working-post-multipart-request-with-volley-and-without-httpentity yet? – BNK Oct 26 '15 at 09:25
  • Yes, but the `byte[]` variable passed into the constructor, how do you get that? And what if there are multiple files to be uploaded? – John Ernest Guadalupe Oct 26 '15 at 12:15
  • 1
    In my sample code, byte array got from `getFileDataFromDrawable`, and I have uploaded 2 files `// the first file buildPart(dos, fileData1, "ic_action_android.png"); // the second file buildPart(dos, fileData2, "ic_action_book.png");` – BNK Oct 26 '15 at 14:23
  • 1
    I really like this solution, let me make a more compact and modular class using your solution. I'll post it a bit later – John Ernest Guadalupe Oct 26 '15 at 23:30
  • Actually, my solution is not perfect, because Google's volley has used Apache's library inside it, as you can easily find `HttpResponse` or `HttpEntity` in BasicNetwork class and others. I also had a question at the following http://stackoverflow.com/questions/32472263/apache-http-client-removal-from-api23-will-that-have-an-effect-on-volley. Pls take a look. – BNK Oct 27 '15 at 05:57

2 Answers2

-1

You are right, MultipartEntity is deprecated, but you can now use MultipartEntityBuilder. Here is an example:

private void uploadVideo(String videoPath) {
    try
    {
        HttpClient client = new DefaultHttpClient();
        File file = new File(videoPath);
        HttpPost post = new HttpPost(server +"/api/docfile/");

        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        entityBuilder.addBinaryBody("uploadfile", file);
        // add more key/value pairs here as needed

        HttpEntity entity = entityBuilder.build();
        post.setEntity(entity);

        HttpResponse response = client.execute(post);
        HttpEntity httpEntity = response.getEntity();

        Log.v("result", EntityUtils.toString(httpEntity));
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Chrome Landos
  • 217
  • 1
  • 3
  • 14
-1

You could have made it clear in your post, if thats the case yes it is,it was deprecated in API 22, you can now use URLconnection, here is a sample":

private StringBuffer request(String urlString) {
// TODO Auto-generated method stub

StringBuffer chaine = new StringBuffer("");
try{
    URL url = new URL(urlString);
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setRequestProperty("User-Agent", "");
    connection.setRequestMethod("POST");
    connection.setDoInput(true);
    connection.connect();

    InputStream inputStream = connection.getInputStream();

    BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
    String line = "";
    while ((line = rd.readLine()) != null) {
        chaine.append(line);
    }

} catch (IOException e) {
    // writing exception to log
    e.printStackTrace();
}

return chaine;

}

Chrome Landos
  • 217
  • 1
  • 3
  • 14