美文网首页
Retrofit上传文件

Retrofit上传文件

作者: 哲狼 | 来源:发表于2019-07-15 18:49 被阅读0次

    MediaType parse = MediaType.parse("application/octet-stream");

    RequestBody cc = RequestBody.create(parse, "cc");

    File file =new File("/storage/sdcard0/Pictures/music1.mp3");

    final RequestBody requestBody = RequestBody.create(parse, file);

    MultipartBody.Part file1 = MultipartBody.Part.createFormData("file", file.getName(), requestBody);

    if (file.exists()) {

    Retrofit retrofit =new Retrofit.Builder()

    .baseUrl("http://yun918.cn/")

    .addConverterFactory(GsonConverterFactory.create())

    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())

    .build();

        ApiSerView apiSerView = retrofit.create(ApiSerView.class);

        Observable getretrofitdata = apiSerView.getretrofitdata(file1, cc);

        getretrofitdata.subscribeOn(Schedulers.newThread())

    .observeOn(AndroidSchedulers.mainThread())

    .subscribe(new Observer() {

    @Override

                    public void onSubscribe(Disposable d) {

    }

    @Override

                    public void onNext(ResponseBody responseBody) {

    try {

    String s = responseBody.string();

                            Log.i("aa", "onNext: " + s);

                        }catch (IOException e) {

    e.printStackTrace();

                        }

    }

    @Override

                    public void onError(Throwable e) {

    }

    @Override

                    public void onComplete() {

    }

    });

    }

    相关文章

      网友评论

          本文标题:Retrofit上传文件

          本文链接:https://www.haomeiwen.com/subject/rnsokctx.html