Post multipart request with Android SDK

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

    You can you use GentleRequest, which is lightweight library for making http requests(DISCLAIMER: I am the author):

    Connections connections = new HttpConnections();
    Binary binary = new PacketsBinary(new 
    BufferedInputStream(new FileInputStream(file)), 
       file.length());
    //Content-Type is set to multipart/form-data; boundary= 
    //{generated by multipart object}
    MultipartForm multipart = new HttpMultipartForm(
        new HttpFormPart("user", "aplication/json", 
           new JSONObject().toString().getBytes()),
        new HttpFormPart("java", "java.png", "image/png", 
           binary.content()));
    Response response = connections.response(new 
        PostRequest(url, multipart));
    if (response.hasSuccessCode()) {
        byte[] raw = response.body().value();
        String string = response.body().stringValue();
        JSONOBject json = response.body().jsonValue();
     } else {
    
     }
    

    Feel free to check it out: https://github.com/Iprogrammerr/Gentle-Request

提交回复
热议问题