Post multipart request with Android SDK

前端 未结 12 1962
余生分开走
余生分开走 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:10

    I can recomend Ion library it use 3 dependences and you can find all three jar files at these two sites:
    https://github.com/koush/ion#jars (ion and androidasync)

    https://code.google.com/p/google-gson/downloads/list (gson)

    try {
       Ion.with(this, "http://www.urlthatyouwant.com/post/page")
       .setMultipartParameter("field1", "This is field number 1")
       .setMultipartParameter("field2", "Field 2 is shorter")
       .setMultipartFile("imagefile",
            new File(Environment.getExternalStorageDirectory()+"/testfile.jpg"))
       .asString()
       .setCallback(new FutureCallback() {
            @Override
            public void onCompleted(Exception e, String result) {
                 System.out.println(result);
            }});
       } catch(Exception e) {
         // Do something about exceptions
            System.out.println("exception: " + e);
       }
    

    this will run async and the callback will be executed in the UI thread once a response is received I strongly recomned that you go to the https://github.com/koush/ion for futher information

提交回复
热议问题