best365体育官网登录入口

跟我学编程:Android Okhttp框架以文件形式上传图片代码

2018/3/10 18:21:11 人评论 次浏览 分类:PLC应用  文章地址://ny-tec.com/tech/1832.html

如何在Android  Okhttp框架以文件形式上传图片?网友“听海”写了这段代码,昌晖仪表在此分享给大家。
计算机编程

以Post为例
public Observable<String> postImageview(final String url, final  MultipartBody requestBody){
  &nbsp;     //创建被观察者
      ;  Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>(){
&nbsp;&nbsp;          @Override
            public void call(Subscriber<? super String> subscriber) {
     &nbsp;          final Subscriber mSubscriber=subscriber;
                //没有取消订阅的时候
                if(!mSubscriber.isUnsubscribed()){
             &nbsp;      //构建post请求
            &nbsp;       Request request=null;
                    String cookie=CliniciansApplication.getOkhttpCookie();
          &nbsp;       &nbsp; if(!BooleanUtils.isEmpty(cookie)){
                     &nbsp;  request=new Request.Builder()
             &nbsp;                  .addHeader("cookie",cookie)
       &nbsp;          &nbsp;             .url(url)
                         &nbsp;      .post(requestBody)
   &nbsp;   &nbsp;                        .build();
        &nbsp;      &nbsp;    }else{
      &nbsp;                 request=new Request.Builder()
               &nbsp;                .url(url)
       &nbsp;                     ;   .post(requestBody)
             ;                   .build();
           &nbsp;        }
     &nbsp;              if(mOkHttpClient!=null){
            &nbsp;   ;        mOkHttpClient.newCall(request).enqueue(new Callback() {
                            @Override
  &nbsp;           &nbsp;             public void onFailure(Call call, IOException e) {
                                //通知订阅者的错误信息
              &nbsp;                 mSubscriber.onError(e);
          ;                ;  }


                            @Override
     ;                       public void onResponse(Call call, Response response) throws IOException {
                          &nbsp;     if(null!=response){//response 不为空
                           &nbsp;        if(response.isSuccessful()){//response 请求成功
            &nbsp;            &nbsp;              //通知订阅者的成功信息
                   &nbsp;                    mSubscriber.onNext(response.body().string());
     &nbsp;                              }else{//response 请求失败
       ;                       ;          //通知订阅者的错误信息
&nbsp;                                 &nbsp;     IOException IOExceptionx=new IOException();
                                        mSubscriber.onError(IOExceptionx);
               &nbsp;                    }
         &nbsp;                      }else{//response 为空
    &nbsp;                             &nbsp; //通知订阅者的错误信息
                                 &nbsp;  IOException IOExceptionx=new IOException();
                                    mSubscriber.onError(IOExceptionx);
   &nbsp;                            }
       &nbsp;                        //通知完毕
    &nbsp;                           mSubscriber.onCompleted();
            &nbsp;               }
          &nbsp;             });
&nbsp; &nbsp;                 }
 ;    &nbsp;          }
        &nbsp;   }
        });
      &nbsp; return observable;
    }

 

调用

private void uploadAva(final String posturl,final File file,final String path){
        if((null!=mRxAndroidOkhttp)&&(null!=file)){
      &nbsp;     showProgressDialogs();
            MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
            builder.addFormDataPart("files", file.getName(), RequestBody.create(MediaType.parse("image/png"), file));
      &nbsp;     builder.addFormDataPart("sePath","avatar");
        ;    MultipartBody requestBody = builder.build();
            //observable定义被观察者
          &nbsp; mObservable=mRxAndroidOkhttp.postImageview(posturl,requestBody);
            if(null!=mObservable){
            ;    //定义观察者
    &nbsp; &nbsp;         Subscriber<String> mSubscriber=new Subscriber<String>(){
                    @Override
         &nbsp;   &nbsp;      public void onCompleted() {


      &nbsp;         &nbsp;   }


              &nbsp;     @Override
      &nbsp;             public void onError(Throwable e) {
      &nbsp;    &nbsp;            dismissProgressDialogs();
     &nbsp;           ;       toast.showToast(StringConstant.errorstate1);
                    }


              &nbsp;   ;  @Override
      &nbsp;             public void onNext(String s) {
               &nbsp;      &nbsp; parseAva(s,path);
&nbsp;                 ;  }
  ;              };


                /**
                ; * 订阅者关联被观察者
              &nbsp;  * Schedulers.io()说明是输入输出的计划任务
        &nbsp;        * AndroidSchedulers.mainThread()说明订阅者是中ui主线程中执行
                 * */


 &nbsp;  &nbsp;           mObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(mSubscriber);
        &nbsp;   }
        }
    }

共有访客发表了评论 网友评论

  客户姓名:
邮箱或QQ:
验证码: 看不清楚?