专注于优质创造,致力于价值传播,记录美好,分享感动!

安卓上传多张图片

2019-1-4 TexRen



//使用MultipartTypedOutput类来上传多张图片
 public void uploadFiles(List<String> paths) {
        MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput();
        for (String imgPath : paths){
        //注意“uploadfile[]”一定要带“[]”,表明上传的是数组,也就是多张图片,不能写“uploadfile”
            multipartTypedOutput.addPart("uploadfile[]", new TypedFile("", new File(imgPath)));
        }
        mApi.uploadFiles(multipartTypedOutput)
                .subscribeOn(Schedulers.computation())
                .subscribe(response -> handleUploadFile(response), error -> handleFailure(error));
    }
    
 private void handleUploadFile(Response response) {

    System.out.println(response.getUrl());
    try {
        InputStream in = response.getBody().in();
        String responseString = inputStream2String(in);
        Log.i("AAA", responseString);
        //解析json数据

        //otto事件传递
        mBus.post(responseString);


    } catch (IOException e) {
        e.printStackTrace();
    }

}

//接收事件代码
 @Subscribe //需要注解@Subscribe ,表明在这个函数接收数据
public void uploadFileResponse(String response) {
    Message msg = JSON.parseObject(response, Message.class);
    List<String> imgUrls = msg.getImgUrls();
    if (msg.getCode() == 1) {
        if (choosePhotoListAdapter == null) {
            choosePhotoListAdapter = new ChoosePhotoListAdapter(this, null, imgUrls);
            lvPhotoShow.setAdapter(choosePhotoListAdapter);
        }
        choosePhotoListAdapter.notifyDataSetChanged();
    }
}


来源:https://www.jianshu.com/p/9847fc8fd5e4

发表评论: