0

I would like to send a file to a server. Currently the code I use includes deprecated classes. How can I update the code to be uptodate?

    protected String doInBackground(String... params) {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), params[0]);
        try {
            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost(server);

            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();
        }
    return null;
    }
  • Duplicate. Please look at http://stackoverflow.com/questions/11766878/sending-files-using-post-with-httpurlconnection – Actiwitty Aug 25 '15 at 18:14

1 Answers1

0

I suggest you to use this library : retrofit http://square.github.io/retrofit/

I use it on my project right now and it helps me a lot with my http requests.

Moreover the documentation is really good.

M. E.
  • 11
  • 5