Giter Site home page Giter Site logo

retrofit2-progress's Introduction

retrofit2-progress

在项目中添加依赖

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
}
dependencies {
	  implementation 'com.github.robining:retrofit2-progress:v0.2-SNAPSHOT'
}

第一步:

添加ProgressCallFactory和ProgressCallAdapterFactory

OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://localhost/") //这里没有使用,因为在Service里面配置的是全路径
        //.client(okHttpClient) //使用callFactory代替
        .callFactory(new ProgressCallFactory(okHttpClient))
        .addCallAdapterFactory(new ProgressCallAdapterFactory())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .addConverterFactory(new StringConvertFactory())
        .build();

第二步:

使用ProgressWrapper封装响应

@GET("https://github.com/robining/retrofit2-progress/new/master?readme=1")
ProgressWrapper<Observable<String>> baiduRx();

@GET("https://github.com/robining/retrofit2-progress/new/master?readme=1")
ProgressWrapper<Call<String>> baiduCall();

第三步:

设置进度监听器

Retrofit原生请求示例
service.baiduCall().setProgressListener(this)
               .getCall()
               .enqueue(new Callback<String>() {
                   @Override
                   public void onResponse(Call<String> call, Response<String> response) {
                       if (response.isSuccessful()) {
                           tvResp.append(response.body() + "\n");
                       } else {
                           tvResp.append("请求失败:" + response.code() + "\n");
                       }
                   }

                   @Override
                   public void onFailure(Call<String> call, Throwable t) {
                       tvResp.append("请求失败:" + t.getMessage() + "\n");
                   }
               });
RxJava请求示例
service.baiduRx().setProgressListener(this)
               .getCall()
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new Observer<String>() {
                   @Override
                   public void onSubscribe(Disposable d) {

                   }

                   @Override
                   public void onNext(String s) {
                       tvResp.append(s + "\n");
                   }

                   @Override
                   public void onError(Throwable e) {
                       tvResp.append("请求失败:" + e.getMessage() + "\n");
                   }

                   @Override
                   public void onComplete() {

                   }
               });
其他类型请求类似

retrofit2-progress's People

Contributors

robining avatar

Stargazers

ChamanZou avatar TSimeon avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.