Post multipart request with Android SDK

前端 未结 12 2047
余生分开走
余生分开走 2020-11-22 04:38

I\'m trying to do something I thought would be relatively simple: Upload an image to a server with the Android SDK. I\'m found a lot of example code:

http://groups.g

12条回答
  •  忘掉有多难
    2020-11-22 05:34

    I highly recommend Loopj.

    I have successfully used it to upload multiple files at once, including different mime types. Simply do this:

    File myVideo = new File("/path/to/myvideo.mp4");
    File myPic = new File("/path/to/mypic.jpg");
    RequestParams params = new RequestParams();
    try {
      params.put("profile_picture", myPic);
      params.put("my_video", myVideo);
    } catch(FileNotFoundException e) {}
    

    For large or many files you might have to increase the timeout amount else the default timeout is used which might be too short:

    client.setTimeout(500000) //make this the appropriate timeout in milliseconds
    

    Please see this links for a full description of loopj and how to use it, by far the easiest async http library I have come across:

    http://loopj.com/android-async-http/ http://loopj.com/android-async-http/doc/com/loopj/android/http/AsyncHttpClient.html

提交回复
热议问题