• 大小: 91KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-16
  • 语言: Java
  • 标签:

资源简介

传统的rxjava和retrofit结合的使用方法

资源截图

代码片段和文件信息

package com.example.retrofit.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.retrofit.R;
import com.example.retrofit.http.HttpService;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends RxAppCompatActivity implements View.onclickListener {
    private TextView tvMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvMsg = (TextView) findViewById(R.id.tv_msg);
        findViewById(R.id.btn_simple).setonclickListener(this);
    }


    @Override
    public void onclick(View v) {
        switch (v.getId()) {
            case R.id.btn_simple:
                onButton9Click();
                break;
        }
    }


    /**
     * Retrofit加入rxjava实现http请求
     */
    private void onButton9Click() {
        //手动创建一个OkHttpClient并设置超时时间
        okhttp3.OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(5 TimeUnit.SECONDS);

        Retrofit retrofit = new Retrofit.Builder()
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(“http://www.izaodao.com/Api/“)
                .build();

//        加载框
        final ProgressDialog pd = new ProgressDialog(this);

        HttpService apiService = retrofit.create(HttpService.class);
        Observable observable = apiService.getAllVedioBy(true);
        observable.subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Subscriber() {
                            @Override
                            public void onCompleted() {
                                if (pd != null && pd.isShowing()) {
                                    pd.dismiss();
                                }
                            }

                            @Override
                            public void onerror(Throwable e) {
                                if (pd != null && pd.isShowing()) {
                                    pd.dismiss();
                                }
                            }

                            @Override
                            public void onNext(RetrofitEntity retrofitEntity) {
       

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        105  2016-10-20 10:56  RxjavaRetrofitDemo-master\.gitignore

     文件          8  2016-10-20 10:56  RxjavaRetrofitDemo-master\app\.gitignore

     文件      13165  2016-10-24 09:09  RxjavaRetrofitDemo-master\app\app.iml

     文件       1441  2016-10-21 14:18  RxjavaRetrofitDemo-master\app\build.gradle

     文件        706  2016-10-20 10:56  RxjavaRetrofitDemo-master\app\proguard-rules.pro

     文件       1034  2016-10-24 09:10  RxjavaRetrofitDemo-master\app\src\main\AndroidManifest.xml

     文件       3452  2016-10-24 09:17  RxjavaRetrofitDemo-master\app\src\main\java\com\example\retrofit\activity\MainActivity.java

     文件        668  2016-10-24 09:14  RxjavaRetrofitDemo-master\app\src\main\java\com\example\retrofit\activity\RetrofitEntity.java

     文件        722  2016-10-24 09:13  RxjavaRetrofitDemo-master\app\src\main\java\com\example\retrofit\activity\Subject.java

     文件        558  2016-10-24 09:17  RxjavaRetrofitDemo-master\app\src\main\java\com\example\retrofit\http\HttpService.java

     文件        898  2016-10-21 13:24  RxjavaRetrofitDemo-master\app\src\main\res\layout\activity_down_laod.xml

     文件        692  2016-10-24 09:11  RxjavaRetrofitDemo-master\app\src\main\res\layout\activity_main.xml

     文件       1414  2016-10-21 13:44  RxjavaRetrofitDemo-master\app\src\main\res\layout\view_item_holder.xml

     文件       3418  2016-10-20 10:56  RxjavaRetrofitDemo-master\app\src\main\res\mipmap-hdpi\ic_launcher.png

     文件       2206  2016-10-20 10:56  RxjavaRetrofitDemo-master\app\src\main\res\mipmap-mdpi\ic_launcher.png

     文件       4842  2016-10-20 10:56  RxjavaRetrofitDemo-master\app\src\main\res\mipmap-xhdpi\ic_launcher.png

     文件       7718  2016-10-20 10:56  RxjavaRetrofitDemo-master\app\src\main\res\mipmap-xxhdpi\ic_launcher.png

     文件      10486  2016-10-20 10:56  RxjavaRetrofitDemo-master\app\src\main\res\mipmap-xxxhdpi\ic_launcher.png

     文件        214  2016-10-20 10:56  RxjavaRetrofitDemo-master\app\src\main\res\values\colors.xml

     文件        216  2016-10-20 10:56  RxjavaRetrofitDemo-master\app\src\main\res\values\dimens.xml

     文件         84  2016-10-20 10:56  RxjavaRetrofitDemo-master\app\src\main\res\values\strings.xml

     文件        394  2016-10-20 10:56  RxjavaRetrofitDemo-master\app\src\main\res\values\styles.xml

     文件        364  2016-10-20 10:56  RxjavaRetrofitDemo-master\app\src\main\res\values-w820dp\dimens.xml

     文件        574  2016-10-20 10:56  RxjavaRetrofitDemo-master\build.gradle

     文件      53636  2016-10-20 10:56  RxjavaRetrofitDemo-master\gradle\wrapper\gradle-wrapper.jar

     文件        237  2016-10-20 10:56  RxjavaRetrofitDemo-master\gradle\wrapper\gradle-wrapper.properties

     文件        872  2016-10-20 10:56  RxjavaRetrofitDemo-master\gradle.properties

     文件       5131  2016-10-20 10:56  RxjavaRetrofitDemo-master\gradlew

     文件       2404  2016-10-20 10:56  RxjavaRetrofitDemo-master\gradlew.bat

     文件       1097  2016-10-20 10:56  RxjavaRetrofitDemo-master\LICENSE

............此处省略26个文件信息

评论

共有 条评论

相关资源