1

I would like to add to my Android app the ability to upload/download files to/from a web server.

Not really sure which is the best way. SSH? FTP? Am sure they both have libraries for Java. I get the feeling FTP might be the way to go, but figured I would ask here since am sure there are those of you that have done both and know which is best.

Thanks as always.

Sergio

Daniel Kaplan
  • 701
  • 7
  • 19
  • check below links for upload file: http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106 http://stackoverflow.com/questions/4126625/how-do-i-send-a-file-in-android-from-a-mobile-device-to-server-using-http – Vickyexpert Jul 02 '16 at 05:38

1 Answers1

0

You can use Android-Async-http libraray https://github.com/loopj/android-async-http

See his doc for usage

http://loopj.com/android-async-http/

Gradle

repositories {
mavenCentral() }

dependencies { compile 'com.loopj.android:android-async-http:1.4.9' }

Uploading

Uploading Files with RequestParams

The RequestParams class additionally supports multipart file uploads as follows:

Add an InputStream to the RequestParams to upload:

InputStream myInputStream = blah;
RequestParams params = new RequestParams();
params.put("secret_passwords", myInputStream, "passwords.txt");

Add a File object to the RequestParams to upload:

File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
    params.put("profile_picture", myFile);
} catch(FileNotFoundException e) {}

Add a byte array to the RequestParams to upload:

byte[] myByteArray = blah;
RequestParams params = new RequestParams();
params.put("soundtrack", new ByteArrayInputStream(myByteArray), "she-wolf.mp3");

And then

AsyncHttpClient client = new AsyncHttpClient();
 client.post("https://myendpoint.com", params, responseHandler);

Downloading

Downloading Binary Data with FileAsyncHttpResponseHandler

The FileAsyncHttpResponseHandler class can be used to fetch binary data such as images and other files. For example:

AsyncHttpClient client = new AsyncHttpClient();
client.get("https://example.com/file.png", new FileAsyncHttpResponseHandler(/* Context */ this) {
    @Override
    public void onSuccess(int statusCode, Header[] headers, File response) {
        // Do something with the file `response`
    }
});
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154