0

Since Android developers removed Apache HTTP Client, I am moving to HttpURLConnection. I was able to rewrite all my functions except one. I am sending byte array (image) with name and date. Actually my function allow sending many images at once, but I am sending only one. Until now, I was using MultipartEntityBuilder to build HttpEntity and set it by httpPost.setEntity(entity). However I want to get rid of MultipartEntityBuilder.

Right now I have following code snippet:

public void sendImage(URL url, byte[] image, String name, long date) {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);

    MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
    entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    entityBuilder.addBinaryBody("file[]", image);
    entityBuilder.addTextBody("file_name[]", name);
    entityBuilder.addTextBody("file_size[]", String.valueOf(date));
    HttpEntity entity = entityBuilder.build();

    conn.addRequestProperty(entity.getContentType().getName(), entity.getContentType().getValue());

    OutputStream os = conn.getOutputStream();
    entity.writeTo(os);
    os.close();

    conn.connect();

    // read response code
}

At server side I can receive data like this:

$file = $_POST['file'];
$file_name = $_POST['file_name'];
$file_size = $_POST['file_size'];

I can not change server implementation.

I tried solution from this thread: Sending files using POST with HttpURLConnection But I do not know how to adjust it to my server.

Is there a way to get rid of HttpEntity? I do not want to use other library (Volley, OkHttp or Retrofit) right now. I will probably move to one of them in future.

Thanks for help.

Community
  • 1
  • 1
AJ.
  • 11
  • 7

1 Answers1

0

To sent files to server you need to use MultipartEntity class that you can write as shown in the below.And you can pass data to it as follows..

AndroidMultiPartEntity entity = new AndroidMultiPartEntity();
File sourceFile = new File("yourpathhere");
entity.addPart("userfile", new FileBody(sourceFile));

and you can write your server code as follows

 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost(targetURL);
 httppost.setEntity(entity);

 // Making server call
 HttpResponse responseObj = httpclient.execute(httppost);
 HttpEntity r_entity = responseObj.getEntity();

 int statusCode = responseObj.getStatusLine().getStatusCode();

 if (statusCode == 200) {
  // Server response
     response = EntityUtils.toString(r_entity);
 } else {
     response = "Error occurred! Http Status Code: "+ statusCode;
 }

MultipartEntityClass:-

public class AndroidMultiPartEntity extends MultipartEntity

{

    //private final ProgressListener listener;

    public AndroidMultiPartEntity() {
        super();
        //this.listener = listener;
    }

    public AndroidMultiPartEntity(final HttpMultipartMode mode,
                                  final ProgressListener listener) {
        super(mode);
        //this.listener = listener;
    }

    public AndroidMultiPartEntity(HttpMultipartMode mode, final String boundary,
                                  final Charset charset, final ProgressListener listener) {
        super(mode, boundary, charset);
        //this.listener = listener;
    }

    @Override
    public void writeTo(final OutputStream outstream) throws IOException {
        super.writeTo(new CountingOutputStream(outstream));
    }

    public static interface ProgressListener {
        void transferred(long num);
    }

    public static class CountingOutputStream extends FilterOutputStream {

        //  private final ProgressListener listener;
        private long transferred;

        public CountingOutputStream(final OutputStream out) {
            super(out);
//  this.listener = listener;
            this.transferred = 0;
        }

        public void write(byte[] b, int off, int len) throws IOException {
            out.write(b, off, len);
            this.transferred += len;
            //this.listener.transferred(this.transferred);
        }

        public void write(int b) throws IOException {
            out.write(b);
            this.transferred++;
            //this.listener.transferred(this.transferred);
        }
    }
}
Harish
  • 3,122
  • 2
  • 31
  • 46
  • I know how to do this using Apache HTTP Client. To be more clear: I do not want to use classes from "org.apache.http" paclage. – AJ. Feb 25 '16 at 11:37