-1

Does http in Android has a multiple-part-form-request just like the multipartFormRequestWithMethod in AFNetWorking lib from Objective-c? I am using https://github.com/kevinsawicki/http-request

I wonder how to send multipart http request on Android. Any ideas, thanks in advance.

EDIT

I am using this way, adding several jars as suggested by this thread, but turns out it fails somewhere, any ideas?

private void uploadBlocksCommit(String commiturl, boolean update, String _uploadpath, String name, int fileSize, List<String> allblocks) throws IOException {
    HttpRequest req = prepareApiPostRequest(commiturl, true, null);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addTextBody("csrfmiddlewaretoken", "n8ba38951c9ba66418311a25195e2e380");
    if (update) {
        builder.addTextBody("replace", "1");
    }
    builder.addTextBody("parent_dir", _uploadpath);
    builder.addTextBody("file_name", name);
    builder.addTextBody("file_size", String.valueOf(fileSize));
    builder.addTextBody("csrfmiddlewaretoken", "n8ba38951c9ba66418311a25195e2e380");
    builder.addTextBody("blockids", allblocks.toString()); // don`t assemble strings in allblocks
    HttpEntity entity = builder.build();
    req.send(entity.getContent());
}
Community
  • 1
  • 1
Logan Guo
  • 865
  • 4
  • 17
  • 35

2 Answers2

1

Create a interface using retrofit library 1.9

public interface upload_file {

@Multipart
@POST("/docupload/")
//public void uploadFile(@Header("Authorization") String auth,@Part("file") File photoFile,@Part("id") Integer familyMemberId,@Part("super_id") Integer super_id, Callback<UploadImageResponse> callback);
public void uploadFile(@Header("Authorization") String auth,@Part("file") TypedFile photoFile,@Part("id") Integer familyMemberId,@Part("file_desc") String filename,@Part("pin")String pin, Callback<String> callback);

}

and from your activity. Also convert you file into typed file object.

 RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL).setEndpoint(API).build();
    //TypedFile typedFile = new TypedFile("*/*",new File(filePath));
    TypedFile typedFile = new TypedFile("*/*", new File(resultPath));
    upload_file hit_Api = restAdapter.create(upload_file.class);
    hit_Api.uploadFile(bearer, typedFile, mFamilyMemberIdSelected, mUserEnteredFileName, mUserSavedPin,new
            Callback<String>() {
                @Override
                public void success(String s, Response response) {


                }

                @Override
                public void failure(RetrofitError error) {

                }
            });
Arora
  • 113
  • 10
0

You can use, Android Upload Service

enter image description here

Easily upload files in the background with automatic Android Notification Center progress indication.

Purpose

  • upload files to a server with HTTP multipart/form-data or binary requests
  • handle uploads in the background, even if the device is idle
  • automatically retry failed uploads, with an exponential backoff
  • possibility to automatically delete successfully uploaded files from the device
  • show status in the Android Notification Center.

Setup

Maven

<dependency>
  <groupId>net.gotev</groupId>
  <artifactId>uploadservice</artifactId>
  <version>2.0.1</version>
  <type>aar</type>
</dependency>

Gradle

dependencies {
    compile 'net.gotev:uploadservice:2.0.1@aar'
}

Reference link.

Shoeb Siddique
  • 2,805
  • 1
  • 22
  • 42