On Mar 12, 6:17 pm, AuxOne <tyler.thack...@gmail.com> wrote:
> I have some Android code that can send files to my webserver using an
> HttpsUrlConnection, but when it tries to send larger files I get an
> OutOfMemory exception when opening the OutputStream. Can anyone offer
> some assistance?

I sent this to the group, but it may take a while for my post to get
approved.

I would try using the Apache HttpClient instead of HttpsURLConnection.


package khuber.sendlargefile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class SendLargeFile extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Sender s = new Sender();
        s.start();
    }

    final Handler handler = new Handler();

    final Runnable showMessage = new Runnable() {
        public void run() {
            message();
        }
    };

    String message;

    public void message() {
        TextView tv = new TextView(this);
        tv.setText("response=" + message);
        setContentView(tv);
    }

    class Sender extends Thread {
        public void run() {
                String msg = "";

                String url = "http://192.168.0.128:8080/PostDemo/
PostServlet";
            File file = new
File(Environment.getExternalStorageDirectory(),
"test2.dat");
            try {
                HttpClient httpclient = new DefaultHttpClient();

                HttpPost httppost = new HttpPost(url);

                Log.i("SendLargeFile", "before send");
                Log.i("SendLargeFile", "file length = " +
file.length());

                InputStreamEntity reqEntity = new InputStreamEntity(
                        new FileInputStream(file), -1);
                reqEntity.setContentType("binary/octet-stream");
                reqEntity.setChunked(true);
                httppost.setEntity(reqEntity);
                Log.i("SendLargeFile", "before execute");
                HttpResponse response = httpclient.execute(httppost);
                Log.i("SendLargeFile", "response = " +
response.getStatusLine());

                HttpEntity resEntity =
response.getEntity();
                StringBuilder sb = new StringBuilder();
                if (resEntity != null) {
                    byte[] buf = new byte[512];
                    InputStream is = resEntity.getContent();
                    int n = 0;
                    while ((n = is.read(buf)) > 0) {
                        sb.append(new String(buf, 0, n));
                    }
                    is.close();
                    resEntity.consumeContent();
                }
                msg = sb.toString();

            } catch (Exception e) {
                msg = e.toString();
            }

            message = msg;
            handler.post(showMessage);
        }
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to