GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - robining/retrofit2-progress

BranchesTags
Open more actions menu

Latest commit

 

History

8 Commits
8 Commits

Folders and files

NameName
Last commit message
Last commit date
.idea
.idea
 
 
app
app
 
 
gradle/wrapper
gradle/wrapper
 
 
library
library
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 
build.gradle
build.gradle
 
 
gradle.properties
gradle.properties
 
 
gradlew
gradlew
 
 
gradlew.bat
gradlew.bat
 
 
settings.gradle
settings.gradle
 
 

Repository files navigation

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() {

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

About

No description, website, or topics provided.

Resources

Readme
Activity

Stars

2 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.