Android Retrofit和Rxjava的网络请求

Android  Retrofit和Rxjava的网络请求

去年的时候好多公司就已经使用Rxjava和Retrofit了,最近自自己学习了一下,感觉真的是很好用,让自己的网络请求变得更简单了,而且封装性极强。

首先做一下准备工作,导入需要引用的文件

compile 'com.android.support:appcompat-v7:25.1.0'
 testCompile 'junit:junit:4.12'
 compile 'io.reactivex:rxjava:1.1.0'
 compile 'io.reactivex:rxandroid:1.1.0'
 compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
 compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
 compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
 compile 'com.google.code.gson:gson:2.6.2'
 compile 'com.jakewharton:butterknife:7.0.1'
 compile 'com.android.support:recyclerview-v7:25.1.0'

网络请求的地址:https://api.douban.com/v2/movie/top250?start=0&count=10

添加网络请求权限

<uses-permission android:name="android.permission.INTERNET"/>

首先看一下Retrofit的使用基本用法

创建一个接口类MovieService 对网络接口进行请求这里写代码片

构建网络请求

public interface MovieService {
 @GET("top250")
 Call<MovieBean> getTopMovie(@Query("start")int start, @Query("count")int count);
}
String baseUrl="https://api.douban.com/v2/movie/";
  Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(baseUrl)
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    .build();
  MovieService service = retrofit.create(MovieService.class);
  service.getTopMovie(0,10)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Subscriber<MovieBean>() {
     @Override
     public void onCompleted() {
      Toast.makeText(MainActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
     }

     @Override
     public void onError(Throwable e) {
      resultTV.setText(e.getMessage());
     }

     @Override
     public void onNext(MovieBean movieBean) {
      resultTV.setText(movieBean.getTitle());
     }
    });
  Call<MovieBean> call = service.getTopMovie(0, 10);
  call.enqueue(new Callback<MovieBean>() {
   @Override
   public void onResponse(Call<MovieBean> call, Response<MovieBean> response) {
    MovieBean body = response.body();
    resultTV.setText(response.body().getTitle());
    System.out.println("成功"+response.body().getCount());
   }

   @Override
   public void onFailure(Call<MovieBean> call, Throwable t) {
    resultTV.setText(t.getMessage());
   }
  });

现在看看我们的布局文件MainActivity的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="ledon.com.retrofitdemo.MainActivity">

 <Button
  android:id="@+id/click_me_BN"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:padding="5dp"
  android:text="获取数据"
  android:textSize="16sp"/>
 <ListView
  android:id="@+id/result_TV"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_above="@id/click_me_BN"
  android:textSize="16sp"/>
</RelativeLayout>

接下来就看看怎么样和RxJava的完美结合了。

首先改造一下MovieService这个接口

public interface MovieService {
 @GET("top250")
 Observable<MovieBean> getTopMovie(@Query("start")int start, @Query("count")int count);
}

然后看一下我们需要创建一个HttpMethods关于一个http的请求方法

package ledon.com.retrofitdemo.api;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.security.auth.Subject;

import ledon.com.retrofitdemo.MovieBean;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/3/9.
 */

public class HttpMethods {
 public static final String BASE_URL="https://api.douban.com/v2/movie/";
 private static final int DEFAULT_TIMEOUT=5;
 private Retrofit retrofit;
 private MovieService movieService;
 //私有化构造方法
 private HttpMethods(){
  //手动创建一个okhttpClient并设置超时时间
  OkHttpClient.Builder builder = new OkHttpClient.Builder();
  builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
  retrofit=new Retrofit.Builder()
    .client(builder.build())
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    .baseUrl(BASE_URL)
    .build();
  movieService = retrofit.create(MovieService.class);

 }
 //在访问httpmethods时候创建单例
 private static class SingletonHolder{
  private static final HttpMethods INSTANCE=new HttpMethods();
 }
  //获取单例
 public static HttpMethods getInstance(){
  return SingletonHolder.INSTANCE;
 }
 public void getTopMovie(Subscriber<MovieBean>subscriber, int start, int count){
  movieService.getTopMovie(start, count)
    .subscribeOn(Schedulers.io())
    .unsubscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(subscriber);

 }

}

so,很简单,我们的结合之路已经完成,另附源代码

package ledon.com.retrofitdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ledon.com.retrofitdemo.adapter.MovieAdapter;
import ledon.com.retrofitdemo.api.HttpMethods;
import rx.Subscriber;

public class MainActivity extends AppCompatActivity {

 @Bind(R.id.click_me_BN)
 Button clickMeBN;
 @Bind(R.id.result_TV)
 ListView resultTV;
 @Bind(R.id.activity_main)
 RelativeLayout activityMain;
 private Subscriber<MovieBean> subscriber;
 private List<MovieBean.SubjectsBean> list;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ButterKnife.bind(this);

  //resultTV.setAdapter();

 }

 //进行网络请求
 private void getMovie() {
  subscriber = new Subscriber<MovieBean>() {
   @Override
   public void onCompleted() {
    Toast.makeText(MainActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
   }

   @Override
   public void onError(Throwable e) {
    //resultTV.setText(e.getMessage());
   }

   @Override
   public void onNext(MovieBean movieBean) {
    // List<MovieBean.SubjectsBean> subjects = movieBean.getSubjects();
    list=movieBean.getSubjects();
    MovieAdapter movieAdapter=new MovieAdapter(MainActivity.this,list);
    resultTV.setAdapter(movieAdapter);
    //resultTV.setText(movieBean.getTitle());
   }
  };
  HttpMethods.getInstance().getTopMovie(subscriber, 0, 10);
  /* String baseUrl="https://api.douban.com/v2/movie/";
  Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(baseUrl)
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    .build();
  MovieService service = retrofit.create(MovieService.class);
  service.getTopMovie(0,10)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Subscriber<MovieBean>() {
     @Override
     public void onCompleted() {
      Toast.makeText(MainActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
     }

     @Override
     public void onError(Throwable e) {
      resultTV.setText(e.getMessage());
     }

     @Override
     public void onNext(MovieBean movieBean) {
      resultTV.setText(movieBean.getTitle());
     }
    });*/
  //Call<MovieBean> call = service.getTopMovie(0, 10);
  /* call.enqueue(new Callback<MovieBean>() {
   @Override
   public void onResponse(Call<MovieBean> call, Response<MovieBean> response) {
    MovieBean body = response.body();
    resultTV.setText(response.body().getTitle());
    System.out.println("成功"+response.body().getCount());
   }

   @Override
   public void onFailure(Call<MovieBean> call, Throwable t) {
    resultTV.setText(t.getMessage());
   }
  });*/
 }

 @OnClick(R.id.click_me_BN)
 public void onClick() {
  getMovie();
 }

}
package ledon.com.retrofitdemo;

import java.util.List;

/**
 * Created by Administrator on 2017/3/9.
 */

public class MovieBean {

 /**
  * count : 10
  * start : 0
  * total : 250
  * subjects : [{"rating":{"max":10,"average":9.6,"stars":"50","min":0},"genres":["犯罪","剧情"],"title":"肖申克的救赎","casts":[{"alt":"https://movie.douban.com/celebrity/1054521/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/17525.jpg","large":"https://img3.doubanio.com/img/celebrity/large/17525.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/17525.jpg"},"name":"蒂姆·罗宾斯","id":"1054521"},{"alt":"https://movie.douban.com/celebrity/1054534/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/34642.jpg","large":"https://img3.doubanio.com/img/celebrity/large/34642.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/34642.jpg"},"name":"摩根·弗里曼","id":"1054534"},{"alt":"https://movie.douban.com/celebrity/1041179/","avatars":{"small":"https://img1.doubanio.com/img/celebrity/small/5837.jpg","large":"https://img1.doubanio.com/img/celebrity/large/5837.jpg","medium":"https://img1.doubanio.com/img/celebrity/medium/5837.jpg"},"name":"鲍勃·冈顿","id":"1041179"}],"collect_count":1036030,"original_title":"The Shawshank Redemption","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1047973/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/230.jpg","large":"https://img3.doubanio.com/img/celebrity/large/230.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/230.jpg"},"name":"弗兰克·德拉邦特","id":"1047973"}],"year":"1994","images":{"small":"https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p480747492.jpg","large":"https://img3.doubanio.com/view/movie_poster_cover/lpst/public/p480747492.jpg","medium":"https://img3.doubanio.com/view/movie_poster_cover/spst/public/p480747492.jpg"},"alt":"https://movie.douban.com/subject/1292052/","id":"1292052"},{"rating":{"max":10,"average":9.4,"stars":"50","min":0},"genres":["剧情","动作","犯罪"],"title":"这个杀手不太冷","casts":[{"alt":"https://movie.douban.com/celebrity/1025182/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/8833.jpg","large":"https://img3.doubanio.com/img/celebrity/large/8833.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/8833.jpg"},"name":"让·雷诺","id":"1025182"},{"alt":"https://movie.douban.com/celebrity/1054454/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/2274.jpg","large":"https://img3.doubanio.com/img/celebrity/large/2274.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/2274.jpg"},"name":"娜塔莉·波特曼","id":"1054454"},{"alt":"https://movie.douban.com/celebrity/1010507/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/104.jpg","large":"https://img3.doubanio.com/img/celebrity/large/104.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/104.jpg"},"name":"加里·奥德曼","id":"1010507"}],"collect_count":1002125,"original_title":"Léon","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1031876/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/33301.jpg","large":"https://img3.doubanio.com/img/celebrity/large/33301.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/33301.jpg"},"name":"吕克·贝松","id":"1031876"}],"year":"1994","images":{"small":"https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p511118051.jpg","large":"https://img3.doubanio.com/view/movie_poster_cover/lpst/public/p511118051.jpg","medium":"https://img3.doubanio.com/view/movie_poster_cover/spst/public/p511118051.jpg"},"alt":"https://movie.douban.com/subject/1295644/","id":"1295644"},{"rating":{"max":10,"average":9.5,"stars":"50","min":0},"genres":["剧情","爱情","同性"],"title":"霸王别姬","casts":[{"alt":"https://movie.douban.com/celebrity/1003494/","avatars":{"small":"https://img1.doubanio.com/img/celebrity/small/67.jpg","large":"https://img1.doubanio.com/img/celebrity/large/67.jpg","medium":"https://img1.doubanio.com/img/celebrity/medium/67.jpg"},"name":"张国荣","id":"1003494"},{"alt":"https://movie.douban.com/celebrity/1050265/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/10381.jpg","large":"https://img3.doubanio.com/img/celebrity/large/10381.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/10381.jpg"},"name":"张丰毅","id":"1050265"},{"alt":"https://movie.douban.com/celebrity/1035641/","avatars":{"small":"https://img1.doubanio.com/img/celebrity/small/1399268395.47.jpg","large":"https://img1.doubanio.com/img/celebrity/large/1399268395.47.jpg","medium":"https://img1.doubanio.com/img/celebrity/medium/1399268395.47.jpg"},"name":"巩俐","id":"1035641"}],"collect_count":727900,"original_title":"霸王别姬","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1023040/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/750.jpg","large":"https://img3.doubanio.com/img/celebrity/large/750.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/750.jpg"},"name":"陈凯歌","id":"1023040"}],"year":"1993","images":{"small":"https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p1910813120.jpg","large":"https://img3.doubanio.com/view/movie_poster_cover/lpst/public/p1910813120.jpg","medium":"https://img3.doubanio.com/view/movie_poster_cover/spst/public/p1910813120.jpg"},"alt":"https://movie.douban.com/subject/1291546/","id":"1291546"},{"rating":{"max":10,"average":9.4,"stars":"50","min":0},"genres":["剧情","爱情"],"title":"阿甘正传","casts":[{"alt":"https://movie.douban.com/celebrity/1054450/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/551.jpg","large":"https://img3.doubanio.com/img/celebrity/large/551.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/551.jpg"},"name":"汤姆·汉克斯","id":"1054450"},{"alt":"https://movie.douban.com/celebrity/1002676/","avatars":{"small":"https://img1.doubanio.com/img/celebrity/small/51737.jpg","large":"https://img1.doubanio.com/img/celebrity/large/51737.jpg","medium":"https://img1.doubanio.com/img/celebrity/medium/51737.jpg"},"name":"罗宾·怀特","id":"1002676"},{"alt":"https://movie.douban.com/celebrity/1031848/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/1345.jpg","large":"https://img3.doubanio.com/img/celebrity/large/1345.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/1345.jpg"},"name":"加里·西尼斯","id":"1031848"}],"collect_count":895393,"original_title":"Forrest Gump","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1053564/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/505.jpg","large":"https://img3.doubanio.com/img/celebrity/large/505.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/505.jpg"},"name":"罗伯特·泽米吉斯","id":"1053564"}],"year":"1994","images":{"small":"https://img1.doubanio.com/view/movie_poster_cover/ipst/public/p510876377.jpg","large":"https://img1.doubanio.com/view/movie_poster_cover/lpst/public/p510876377.jpg","medium":"https://img1.doubanio.com/view/movie_poster_cover/spst/public/p510876377.jpg"},"alt":"https://movie.douban.com/subject/1292720/","id":"1292720"},{"rating":{"max":10,"average":9.5,"stars":"50","min":0},"genres":["剧情","喜剧","爱情"],"title":"美丽人生","casts":[{"alt":"https://movie.douban.com/celebrity/1041004/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/26764.jpg","large":"https://img3.doubanio.com/img/celebrity/large/26764.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/26764.jpg"},"name":"罗伯托·贝尼尼","id":"1041004"},{"alt":"https://movie.douban.com/celebrity/1000375/","avatars":{"small":"https://img1.doubanio.com/img/celebrity/small/9548.jpg","large":"https://img1.doubanio.com/img/celebrity/large/9548.jpg","medium":"https://img1.doubanio.com/img/celebrity/medium/9548.jpg"},"name":"尼可莱塔·布拉斯基","id":"1000375"},{"alt":"https://movie.douban.com/celebrity/1000368/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/45590.jpg","large":"https://img3.doubanio.com/img/celebrity/large/45590.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/45590.jpg"},"name":"乔治·坎塔里尼","id":"1000368"}],"collect_count":493063,"original_title":"La vita è bella","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1041004/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/26764.jpg","large":"https://img3.doubanio.com/img/celebrity/large/26764.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/26764.jpg"},"name":"罗伯托·贝尼尼","id":"1041004"}],"year":"1997","images":{"small":"https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p510861873.jpg","large":"https://img3.doubanio.com/view/movie_poster_cover/lpst/public/p510861873.jpg","medium":"https://img3.doubanio.com/view/movie_poster_cover/spst/public/p510861873.jpg"},"alt":"https://movie.douban.com/subject/1292063/","id":"1292063"},{"rating":{"max":10,"average":9.2,"stars":"50","min":0},"genres":["剧情","动画","奇幻"],"title":"千与千寻","casts":[{"alt":"https://movie.douban.com/celebrity/1023337/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/1463193210.13.jpg","large":"https://img3.doubanio.com/img/celebrity/large/1463193210.13.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/1463193210.13.jpg"},"name":"柊瑠美","id":"1023337"},{"alt":"https://movie.douban.com/celebrity/1005438/","avatars":{"small":"https://img5.doubanio.com/img/celebrity/small/44986.jpg","large":"https://img5.doubanio.com/img/celebrity/large/44986.jpg","medium":"https://img5.doubanio.com/img/celebrity/medium/44986.jpg"},"name":"入野自由","id":"1005438"},{"alt":"https://movie.douban.com/celebrity/1045797/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/18785.jpg","large":"https://img3.doubanio.com/img/celebrity/large/18785.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/18785.jpg"},"name":"夏木真理","id":"1045797"}],"collect_count":801778,"original_title":"千と千尋の神隠し","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1054439/","avatars":{"small":"https://img5.doubanio.com/img/celebrity/small/616.jpg","large":"https://img5.doubanio.com/img/celebrity/large/616.jpg","medium":"https://img5.doubanio.com/img/celebrity/medium/616.jpg"},"name":"宫崎骏","id":"1054439"}],"year":"2001","images":{"small":"https://img5.doubanio.com/view/movie_poster_cover/ipst/public/p1910830216.jpg","large":"https://img5.doubanio.com/view/movie_poster_cover/lpst/public/p1910830216.jpg","medium":"https://img5.doubanio.com/view/movie_poster_cover/spst/public/p1910830216.jpg"},"alt":"https://movie.douban.com/subject/1291561/","id":"1291561"},{"rating":{"max":10,"average":9.4,"stars":"50","min":0},"genres":["剧情","历史","战争"],"title":"辛德勒的名单","casts":[{"alt":"https://movie.douban.com/celebrity/1031220/","avatars":{"small":"https://img5.doubanio.com/img/celebrity/small/44906.jpg","large":"https://img5.doubanio.com/img/celebrity/large/44906.jpg","medium":"https://img5.doubanio.com/img/celebrity/medium/44906.jpg"},"name":"连姆·尼森","id":"1031220"},{"alt":"https://movie.douban.com/celebrity/1054393/","avatars":{"small":"https://img1.doubanio.com/img/celebrity/small/1374649659.58.jpg","large":"https://img1.doubanio.com/img/celebrity/large/1374649659.58.jpg","medium":"https://img1.doubanio.com/img/celebrity/medium/1374649659.58.jpg"},"name":"本·金斯利","id":"1054393"},{"alt":"https://movie.douban.com/celebrity/1006956/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/28941.jpg","large":"https://img3.doubanio.com/img/celebrity/large/28941.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/28941.jpg"},"name":"拉尔夫·费因斯","id":"1006956"}],"collect_count":472445,"original_title":"Schindler's List","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1054440/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/34602.jpg","large":"https://img3.doubanio.com/img/celebrity/large/34602.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/34602.jpg"},"name":"史蒂文·斯皮尔伯格","id":"1054440"}],"year":"1993","images":{"small":"https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p492406163.jpg","large":"https://img3.doubanio.com/view/movie_poster_cover/lpst/public/p492406163.jpg","medium":"https://img3.doubanio.com/view/movie_poster_cover/spst/public/p492406163.jpg"},"alt":"https://movie.douban.com/subject/1295124/","id":"1295124"},{"rating":{"max":10,"average":9.2,"stars":"45","min":0},"genres":["剧情","爱情","灾难"],"title":"泰坦尼克号","casts":[{"alt":"https://movie.douban.com/celebrity/1041029/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/470.jpg","large":"https://img3.doubanio.com/img/celebrity/large/470.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/470.jpg"},"name":"莱昂纳多·迪卡普里奥","id":"1041029"},{"alt":"https://movie.douban.com/celebrity/1054446/","avatars":{"small":"https://img1.doubanio.com/img/celebrity/small/53358.jpg","large":"https://img1.doubanio.com/img/celebrity/large/53358.jpg","medium":"https://img1.doubanio.com/img/celebrity/medium/53358.jpg"},"name":"凯特·温丝莱特","id":"1054446"},{"alt":"https://movie.douban.com/celebrity/1031864/","avatars":{"small":"https://img5.doubanio.com/img/celebrity/small/45186.jpg","large":"https://img5.doubanio.com/img/celebrity/large/45186.jpg","medium":"https://img5.doubanio.com/img/celebrity/medium/45186.jpg"},"name":"比利·赞恩","id":"1031864"}],"collect_count":823041,"original_title":"Titanic","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1022571/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/33715.jpg","large":"https://img3.doubanio.com/img/celebrity/large/33715.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/33715.jpg"},"name":"詹姆斯·卡梅隆","id":"1022571"}],"year":"1997","images":{"small":"https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p457760035.jpg","large":"https://img3.doubanio.com/view/movie_poster_cover/lpst/public/p457760035.jpg","medium":"https://img3.doubanio.com/view/movie_poster_cover/spst/public/p457760035.jpg"},"alt":"https://movie.douban.com/subject/1292722/","id":"1292722"},{"rating":{"max":10,"average":9.2,"stars":"50","min":0},"genres":["剧情","动作","科幻"],"title":"盗梦空间","casts":[{"alt":"https://movie.douban.com/celebrity/1041029/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/470.jpg","large":"https://img3.doubanio.com/img/celebrity/large/470.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/470.jpg"},"name":"莱昂纳多·迪卡普里奥","id":"1041029"},{"alt":"https://movie.douban.com/celebrity/1101703/","avatars":{"small":"https://img1.doubanio.com/img/celebrity/small/3517.jpg","large":"https://img1.doubanio.com/img/celebrity/large/3517.jpg","medium":"https://img1.doubanio.com/img/celebrity/medium/3517.jpg"},"name":"约瑟夫·高登-莱维特","id":"1101703"},{"alt":"https://movie.douban.com/celebrity/1012520/","avatars":{"small":"https://img1.doubanio.com/img/celebrity/small/118.jpg","large":"https://img1.doubanio.com/img/celebrity/large/118.jpg","medium":"https://img1.doubanio.com/img/celebrity/medium/118.jpg"},"name":"艾伦·佩吉","id":"1012520"}],"collect_count":903330,"original_title":"Inception","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1054524/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/673.jpg","large":"https://img3.doubanio.com/img/celebrity/large/673.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/673.jpg"},"name":"克里斯托弗·诺兰","id":"1054524"}],"year":"2010","images":{"small":"https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p513344864.jpg","large":"https://img3.doubanio.com/view/movie_poster_cover/lpst/public/p513344864.jpg","medium":"https://img3.doubanio.com/view/movie_poster_cover/spst/public/p513344864.jpg"},"alt":"https://movie.douban.com/subject/3541415/","id":"3541415"},{"rating":{"max":10,"average":9.3,"stars":"50","min":0},"genres":["喜剧","爱情","科幻"],"title":"机器人总动员","casts":[{"alt":"https://movie.douban.com/celebrity/1009535/","avatars":{"small":"https://img1.doubanio.com/img/celebrity/small/13028.jpg","large":"https://img1.doubanio.com/img/celebrity/large/13028.jpg","medium":"https://img1.doubanio.com/img/celebrity/medium/13028.jpg"},"name":"本·贝尔特","id":"1009535"},{"alt":"https://movie.douban.com/celebrity/1000389/","avatars":{"small":"https://img5.doubanio.com/img/celebrity/small/1365856130.16.jpg","large":"https://img5.doubanio.com/img/celebrity/large/1365856130.16.jpg","medium":"https://img5.doubanio.com/img/celebrity/medium/1365856130.16.jpg"},"name":"艾丽莎·奈特","id":"1000389"},{"alt":"https://movie.douban.com/celebrity/1018022/","avatars":{"small":"https://img1.doubanio.com/img/celebrity/small/31068.jpg","large":"https://img1.doubanio.com/img/celebrity/large/31068.jpg","medium":"https://img1.doubanio.com/img/celebrity/medium/31068.jpg"},"name":"杰夫·格尔林","id":"1018022"}],"collect_count":586255,"original_title":"WALL·E","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1036450/","avatars":{"small":"https://img5.doubanio.com/img/celebrity/small/1467359656.96.jpg","large":"https://img5.doubanio.com/img/celebrity/large/1467359656.96.jpg","medium":"https://img5.doubanio.com/img/celebrity/medium/1467359656.96.jpg"},"name":"安德鲁·斯坦顿","id":"1036450"}],"year":"2008","images":{"small":"https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p449665982.jpg","large":"https://img3.doubanio.com/view/movie_poster_cover/lpst/public/p449665982.jpg","medium":"https://img3.doubanio.com/view/movie_poster_cover/spst/public/p449665982.jpg"},"alt":"https://movie.douban.com/subject/2131459/","id":"2131459"}]
  * title : 豆瓣电影Top250
  */

 private int count;
 private int start;
 private int total;
 private String title;
 private List<SubjectsBean> subjects;

 public int getCount() {
  return count;
 }

 public void setCount(int count) {
  this.count = count;
 }

 public int getStart() {
  return start;
 }

 public void setStart(int start) {
  this.start = start;
 }

 public int getTotal() {
  return total;
 }

 public void setTotal(int total) {
  this.total = total;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public List<SubjectsBean> getSubjects() {
  return subjects;
 }

 public void setSubjects(List<SubjectsBean> subjects) {
  this.subjects = subjects;
 }

 public static class SubjectsBean {
  /**
   * rating : {"max":10,"average":9.6,"starsmin":0}
   * genres : ["犯罪","剧情"]":"50","
   * title : 肖申克的救赎
   * casts : [{"alt":"https://movie.douban.com/celebrity/1054521/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/17525.jpg","large":"https://img3.doubanio.com/img/celebrity/large/17525.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/17525.jpg"},"name":"蒂姆·罗宾斯","id":"1054521"},{"alt":"https://movie.douban.com/celebrity/1054534/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/34642.jpg","large":"https://img3.doubanio.com/img/celebrity/large/34642.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/34642.jpg"},"name":"摩根·弗里曼","id":"1054534"},{"alt":"https://movie.douban.com/celebrity/1041179/","avatars":{"small":"https://img1.doubanio.com/img/celebrity/small/5837.jpg","large":"https://img1.doubanio.com/img/celebrity/large/5837.jpg","medium":"https://img1.doubanio.com/img/celebrity/medium/5837.jpg"},"name":"鲍勃·冈顿","id":"1041179"}]
   * collect_count : 1036030
   * original_title : The Shawshank Redemption
   * subtype : movie
   * directors : [{"alt":"https://movie.douban.com/celebrity/1047973/","avatars":{"small":"https://img3.doubanio.com/img/celebrity/small/230.jpg","large":"https://img3.doubanio.com/img/celebrity/large/230.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/230.jpg"},"name":"弗兰克·德拉邦特","id":"1047973"}]
   * year : 1994
   * images : {"small":"https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p480747492.jpg","large":"https://img3.doubanio.com/view/movie_poster_cover/lpst/public/p480747492.jpg","medium":"https://img3.doubanio.com/view/movie_poster_cover/spst/public/p480747492.jpg"}
   * alt : https://movie.douban.com/subject/1292052/
   * id : 1292052
   */

  private RatingBean rating;
  private String title;
  private int collect_count;
  private String original_title;
  private String subtype;
  private String year;
  private ImagesBean images;
  private String alt;
  private String id;
  private List<String> genres;
  private List<CastsBean> casts;
  private List<DirectorsBean> directors;

  public RatingBean getRating() {
   return rating;
  }

  public void setRating(RatingBean rating) {
   this.rating = rating;
  }

  public String getTitle() {
   return title;
  }

  public void setTitle(String title) {
   this.title = title;
  }

  public int getCollect_count() {
   return collect_count;
  }

  public void setCollect_count(int collect_count) {
   this.collect_count = collect_count;
  }

  public String getOriginal_title() {
   return original_title;
  }

  public void setOriginal_title(String original_title) {
   this.original_title = original_title;
  }

  public String getSubtype() {
   return subtype;
  }

  public void setSubtype(String subtype) {
   this.subtype = subtype;
  }

  public String getYear() {
   return year;
  }

  public void setYear(String year) {
   this.year = year;
  }

  public ImagesBean getImages() {
   return images;
  }

  public void setImages(ImagesBean images) {
   this.images = images;
  }

  public String getAlt() {
   return alt;
  }

  public void setAlt(String alt) {
   this.alt = alt;
  }

  public String getId() {
   return id;
  }

  public void setId(String id) {
   this.id = id;
  }

  public List<String> getGenres() {
   return genres;
  }

  public void setGenres(List<String> genres) {
   this.genres = genres;
  }

  public List<CastsBean> getCasts() {
   return casts;
  }

  public void setCasts(List<CastsBean> casts) {
   this.casts = casts;
  }

  public List<DirectorsBean> getDirectors() {
   return directors;
  }

  public void setDirectors(List<DirectorsBean> directors) {
   this.directors = directors;
  }

  public static class RatingBean {
   /**
    * max : 10
    * average : 9.6
    * stars : 50
    * min : 0
    */

   private int max;
   private double average;
   private String stars;
   private int min;

   public int getMax() {
    return max;
   }

   public void setMax(int max) {
    this.max = max;
   }

   public double getAverage() {
    return average;
   }

   public void setAverage(double average) {
    this.average = average;
   }

   public String getStars() {
    return stars;
   }

   public void setStars(String stars) {
    this.stars = stars;
   }

   public int getMin() {
    return min;
   }

   public void setMin(int min) {
    this.min = min;
   }
  }

  public static class ImagesBean {
   /**
    * small : https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p480747492.jpg
    * large : https://img3.doubanio.com/view/movie_poster_cover/lpst/public/p480747492.jpg
    * medium : https://img3.doubanio.com/view/movie_poster_cover/spst/public/p480747492.jpg
    */

   private String small;
   private String large;
   private String medium;

   public String getSmall() {
    return small;
   }

   public void setSmall(String small) {
    this.small = small;
   }

   public String getLarge() {
    return large;
   }

   public void setLarge(String large) {
    this.large = large;
   }

   public String getMedium() {
    return medium;
   }

   public void setMedium(String medium) {
    this.medium = medium;
   }
  }

  public static class CastsBean {
   /**
    * alt : https://movie.douban.com/celebrity/1054521/
    * avatars : {"small":"https://img3.doubanio.com/img/celebrity/small/17525.jpg","large":"https://img3.doubanio.com/img/celebrity/large/17525.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/17525.jpg"}
    * name : 蒂姆·罗宾斯
    * id : 1054521
    */

   private String alt;
   private AvatarsBean avatars;
   private String name;
   private String id;

   public String getAlt() {
    return alt;
   }

   public void setAlt(String alt) {
    this.alt = alt;
   }

   public AvatarsBean getAvatars() {
    return avatars;
   }

   public void setAvatars(AvatarsBean avatars) {
    this.avatars = avatars;
   }

   public String getName() {
    return name;
   }

   public void setName(String name) {
    this.name = name;
   }

   public String getId() {
    return id;
   }

   public void setId(String id) {
    this.id = id;
   }

   public static class AvatarsBean {
    /**
     * small : https://img3.doubanio.com/img/celebrity/small/17525.jpg
     * large : https://img3.doubanio.com/img/celebrity/large/17525.jpg
     * medium : https://img3.doubanio.com/img/celebrity/medium/17525.jpg
     */

    private String small;
    private String large;
    private String medium;

    public String getSmall() {
     return small;
    }

    public void setSmall(String small) {
     this.small = small;
    }

    public String getLarge() {
     return large;
    }

    public void setLarge(String large) {
     this.large = large;
    }

    public String getMedium() {
     return medium;
    }

    public void setMedium(String medium) {
     this.medium = medium;
    }
   }
  }

  public static class DirectorsBean {
   /**
    * alt : https://movie.douban.com/celebrity/1047973/
    * avatars : {"small":"https://img3.doubanio.com/img/celebrity/small/230.jpg","large":"https://img3.doubanio.com/img/celebrity/large/230.jpg","medium":"https://img3.doubanio.com/img/celebrity/medium/230.jpg"}
    * name : 弗兰克·德拉邦特
    * id : 1047973
    */

   private String alt;
   private AvatarsBeanX avatars;
   private String name;
   private String id;

   public String getAlt() {
    return alt;
   }

   public void setAlt(String alt) {
    this.alt = alt;
   }

   public AvatarsBeanX getAvatars() {
    return avatars;
   }

   public void setAvatars(AvatarsBeanX avatars) {
    this.avatars = avatars;
   }

   public String getName() {
    return name;
   }

   public void setName(String name) {
    this.name = name;
   }

   public String getId() {
    return id;
   }

   public void setId(String id) {
    this.id = id;
   }

   public static class AvatarsBeanX {
    /**
     * small : https://img3.doubanio.com/img/celebrity/small/230.jpg
     * large : https://img3.doubanio.com/img/celebrity/large/230.jpg
     * medium : https://img3.doubanio.com/img/celebrity/medium/230.jpg
     */

    private String small;
    private String large;
    private String medium;

    public String getSmall() {
     return small;
    }

    public void setSmall(String small) {
     this.small = small;
    }

    public String getLarge() {
     return large;
    }

    public void setLarge(String large) {
     this.large = large;
    }

    public String getMedium() {
     return medium;
    }

    public void setMedium(String medium) {
     this.medium = medium;
    }
   }
  }
 }
}
package ledon.com.retrofitdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import ledon.com.retrofitdemo.MovieBean;
import ledon.com.retrofitdemo.R;

/**
 * Created by Administrator on 2017/3/9.
 */

public class MovieAdapter extends BaseAdapter {
 private Context context;
 private List<MovieBean.SubjectsBean> lists;

 public MovieAdapter(Context context, List<MovieBean.SubjectsBean> lists) {
  this.context = context;
  this.lists = lists;
 }

 @Override
 public int getCount() {
  System.out.println("集合大小:"+lists.size());
  return lists.size();
 }

 @Override
 public Object getItem(int i) {
  return null;
 }

 @Override
 public long getItemId(int i) {
  return 0;
 }

 @Override
 public View getView(int i, View convertView, ViewGroup viewGroup) {
  ViewHolder holder;
  if (convertView==null){
   convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
   holder=new ViewHolder();
   holder.tv_title= (TextView) convertView.findViewById(R.id.tv_title);
   holder.tv_name= (TextView) convertView.findViewById(R.id.tv_name);
   convertView.setTag(holder);
  }else {
   holder= (ViewHolder) convertView.getTag();
  }

  //MovieBean.SubjectsBean subjectsBean = lists.get(i).getSubjects().get(i);

  holder.tv_title.setText(lists.get(i).getTitle());
//  holder.tv_name.setText(lists.get(i).getCasts().get(i).getName());
  return convertView;
 }
 static class ViewHolder{
  TextView tv_title;
  TextView tv_name;
 }
}
package ledon.com.retrofitdemo.api;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.security.auth.Subject;

import ledon.com.retrofitdemo.MovieBean;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/3/9.
 */

public class HttpMethods {
 public static final String BASE_URL="https://api.douban.com/v2/movie/";
 private static final int DEFAULT_TIMEOUT=5;
 private Retrofit retrofit;
 private MovieService movieService;
 //私有化构造方法
 private HttpMethods(){
  //手动创建一个okhttpClient并设置超时时间
  OkHttpClient.Builder builder = new OkHttpClient.Builder();
  builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
  retrofit=new Retrofit.Builder()
    .client(builder.build())
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    .baseUrl(BASE_URL)
    .build();
  movieService = retrofit.create(MovieService.class);

 }
 //在访问httpmethods时候创建单例
 private static class SingletonHolder{
  private static final HttpMethods INSTANCE=new HttpMethods();
 }
  //获取单例
 public static HttpMethods getInstance(){
  return SingletonHolder.INSTANCE;
 }
 public void getTopMovie(Subscriber<MovieBean>subscriber, int start, int count){
  movieService.getTopMovie(start, count)
    .subscribeOn(Schedulers.io())
    .unsubscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(subscriber);
  /* movieService.getTopMovie(start, count)
    .map(new HttpResultFunc<List<Subject>>())
    .subscribeOn(Schedulers.io())
    .unsubscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(subscriber);*/
 }
 private class HttpResultFunc<T> implements Func1<HttpResult<T>,T>{

  @Override
  public T call(HttpResult<T> httpResult) {

   return httpResult.getSubjects();
  }
 }
}
package ledon.com.retrofitdemo.api;

import ledon.com.retrofitdemo.MovieBean;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
import rx.Observable;

/**
 * Created by Administrator on 2017/3/9.
 */

public interface MovieService {
 @GET("top250")
 Observable<MovieBean> getTopMovie(@Query("start")int start, @Query("count")int count);
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <TextView
  android:layout_gravity="center"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/tv_title"
  android:textSize="20dp"
  android:text="这个杀手不太冷"/>

 <ImageView
  android:layout_gravity="center"
  android:id="@+id/iv"
  android:layout_width="100dp"
  android:layout_height="100dp"
  android:src="@mipmap/ic_launcher"/>
<TextView
 android:id="@+id/tv_name"
 android:layout_gravity="center"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="20dp"
 android:text="主演:呵呵哈哈哈"/>
</LinearLayout>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

(0)

相关推荐

  • Android使用Retrofit仿微信多张图片拍照上传

    Android 仿照微信发说说,既能实现拍照,选图库,多图案上传,使用Retrofit技术. 使用方法:详见http://www.jb51.net/article/103009.htm 项目的运行效果: 服务器端接收文件的action UploadFile.java @Controller public class UploadFile extends ActionSupport { /** * */ private static final long serialVersionUID = 1L

  • Android Retrofit实现多图片/文件、图文上传功能

    什么是 Retrofit ? Retrofit是Square开发的一个Android和Java的REST客户端库.这个库非常简单并且具有很多特性,相比其他的网络库,更容易让初学者快速掌握.它可以处理GET.POST.PUT.DELETE-等请求,还可以使用picasso加载图片. 一.再次膜拜下Retrofit Retrofit无论从性能还是使用方便性上都很屌!!!,本文不去介绍其运作原理(虽然很想搞明白),后面会出专题文章解析Retrofit的内部原理:本文只是从使用上解析Retrofit实现

  • Android Retrofit 中文乱码问题的解决办法

    Android Retrofit 中文乱码问题的解决办法 使用retrofit和rxjava,提交数据时需注意,当数据中有中文时,传到后台,可能会是乱码,需处理: 解决: 1.GET请求改成POST; 2.参数Field改成Query 3.加上@FormUrlEncoded 如下: @FormUrlEncoded @POST("/test/test") Call<Response> register(@Field("name") String name)

  • Android中Retrofit 2.0直接使用JSON进行数据交互

    之前使用Retrofit都是将JSON串转化为POJO对象,针对不同的业务协议,定义相应的接口和参数列表.但是此种方式一般用在自己内部协议基础上,具体大的项目中,有些第三方的集成功能,一般都采用统一的方式即请求JSON和回应JSON进行数据交互,不可能每个第三方协议都会去定义与协议相应的POJO对象. HTTP肯定有GET和POST方法,先定义Retrofit Api的interface: package com.hdnetworklib.network.http; import java.ut

  • Android中Retrofit+OkHttp进行HTTP网络编程的使用指南

    Retrofit介绍: Retrofit(GitHub主页https://github.com/square/okhttp)和OkHttp师出同门,也是Square的开源库,它是一个类型安全的网络请求库,Retrofit简化了网络请求流程,基于OkHtttp做了封装,解耦的更彻底:比方说通过注解来配置请求参数,通过工厂来生成CallAdapter,Converter,你可以使用不同的请求适配器(CallAdapter), 比方说RxJava,Java8, Guava.你可以使用不同的反序列化工具

  • 简略分析Android的Retrofit应用开发框架源码

    面对一个项目,对于Android应用开发框架的选择,我想过三种方案: 1.使用Loader + HttpClient + GreenDao + Gson + Fragment,优点是可定制性强,由于使用Google家自己的Loader和LoaderManager,代码健壮性强. 缺点是整套代码学习成本较高,使用过程中样板代码较多,(比如每一个Request都需要产生一个新类) 2.Volley,作为Google在IO大会上得瑟过的一个网络库,其实不算什么新东西(2013 IO发布),使用较为简单

  • Android Retrofit 2.0框架上传图片解决方案

    本文为大家分享了 Android Retrofit 2.0框架上传图片解决方案,具体内容如下 1.单张图片的上传 /** * 上传一张图片 * @param description * @param imgs * @return */ @Multipart @POST("/upload") Call<String> uploadImage(@Part("fileName") String description, @Part("file\&qu

  • android Retrofit2+okHttp3使用总结

    使用前准备 Build.gradle文件配置 dependencies配置 compile 'com.squareup.retrofit2:retrofit:2.0.0' compile 'com.squareup.retrofit2:converter-gson:2.0.0' compile 'com.squareup.okhttp3:logging-interceptor:3.2.0' 网络框架搭建 服务创建类封装(HTTP): public class ServiceGenerator {

  • Android app开发中Retrofit框架的初步上手使用

    Retrofit 2.0 先来说一下Retrofit 2.0版本中一些引人注意的地方. 在Retrofit 2.0中,最大的改动莫过于减小库的体积,首先,Retrofit 2.0去掉了对所有的HTTP客户端的兼容,而钟情于OkHttpClient一个,极大地减少了各种适配代码,原因一会儿说;其次,拆库,比如将对RxJava的支持设置为可选(需要额外引入库):再比如将各个序列化反序列化转换器支持设置为可选(需要额外引入库).于2.0抛弃HttpClient和HttpURLConnection,为了

  • Android Retrofit和Rxjava的网络请求

    Android  Retrofit和Rxjava的网络请求 去年的时候好多公司就已经使用Rxjava和Retrofit了,最近自自己学习了一下,感觉真的是很好用,让自己的网络请求变得更简单了,而且封装性极强. 首先做一下准备工作,导入需要引用的文件 compile 'com.android.support:appcompat-v7:25.1.0' testCompile 'junit:junit:4.12' compile 'io.reactivex:rxjava:1.1.0' compile

  • Android  Retrofit和Rxjava的网络请求

    Android  Retrofit和Rxjava的网络请求 去年的时候好多公司就已经使用Rxjava和Retrofit了,最近自自己学习了一下,感觉真的是很好用,让自己的网络请求变得更简单了,而且封装性极强. 首先做一下准备工作,导入需要引用的文件 compile 'com.android.support:appcompat-v7:25.1.0' testCompile 'junit:junit:4.12' compile 'io.reactivex:rxjava:1.1.0' compile

  • Kotlin结合Rxjava+Retrofit实现极简网络请求的方法

    前言 因为最近正在写的项目集成了两个网络请求框架(Volley and Retrofit)对比之下也是选择了Retrofit.既然选择那自然要让自己以后开发更加省力(就是懒).于是我在Retrofit中加入了Rxjava,这也是当下蛮流行的一个请求框架.然后又利用了Kotlin的一些新特性,使网络请求变得特别简单,代码量特别少. Kotlin镇楼 RxJava RxJava学习是一个曲折漫长的过程,但一旦掌握,妙用无穷. 通过这里了解更多:http://www.jb51.net/article/

  • 如何利用Retrofit+RxJava实现网络请求的异常处理

    通常情况下我们在与服务器进行通信的时候,不一定就不会出错,有时会出现其他的错误,这个时候我们只要和服务器约定好各种异常,在返回结果处进行判断,到底是执行错误,还是返回正常数据.具体的思路大致就是这样.这里我们定义ExceptionHandle,这里我参考网上的东西,然后稍微做了一些改动. ExceptionHandle public class ExceptionHandle { private static final int UNAUTHORIZED = 401; private stati

  • RxJava之网络请求最常见的三种场景

    本文想阐述一下当你开发Android应用并采用RxJava作为你的架构,尤其是有关网络请求时最常见的三种场景. 我使用Retrofit来作为网络层,简单的内存缓存-HashMap来做缓存,也可以使用Room或者其他数据库实现来替代. Retrofit接口有如下的一些简单方法,它获取一个事件列表. @GET("events") Single<List<Event>> getEventsFeed(...); 通过我的Repository接口来暴露,可订阅如下: Si

  • Android最基本的异步网络请求框架

    本篇文章我们来一起写一个最基本的Android异步网络请求框架,借此来了解下Android中网络请求的相关知识.由于个人水平有限,文中难免存在疏忽和谬误,希望大家可以指出,谢谢大家. 1. 同步网络请求 以HTTP的GET请求为例,我们来介绍一下Android中一个基本的同步请求框架的实现.直接贴代码: public class HttpUtils { public static byte[] get(String urlString) { HttpURLConnection urlConnec

  • 深入浅出RxJava+Retrofit+OkHttp网络请求

    浅谈RxJava+Retrofit+OkHttp 封装使用之前发出后收到很多朋友的关注,原本只是自己学习后的一些经验总结,但是有同学运用到实战当中,这让我很惶恐,所有后续一直更新了很多次版本,有些地方难免有所变动导致之前的博客有所出入,正好最近受到掘金邀请内测博客,所以决定重新写一版,按照最后迭代完成的封装详细的讲述一遍,欢迎大家关注! 注意:由于本章的特殊性,后续文章比较长而且复杂,涉及内容也很多,所以大家准备好茶水,前方高能预警. 简介: Retrofit: Retrofit是Square

  • android Retrofit2网络请求封装介绍

    目录 1. Retrofit使用 2. Retrofit封装 3. RetrofitUtil使用 最后 1. Retrofit使用 Retrofit是一个现在网络请求框架,先来说一下怎么使用 网络权限(添加到AndroidManifest.xml) <uses-permission android:name="android.permission.INTERNET" /> gradle依赖(添加到build.gradle) implementation("com.

  • Android Retrofit原理深入探索

    目录 序章 Retrofit构建过程 创建网络请求接口实例过程 执行请求过程 总结 序章 首先引入依赖 implementation 'com.squareup.retrofit2:retrofit:2.9.0' 在原理之前,我们先来回忆一下Retrofit的基本使用 1.定义接口 interface MyService { @GET("gallery/{imageType}/response") fun getImages(@Path("imageType") i

  • RxJava+Retrofit实现网络请求封装的方法

    简要介绍 Retrofit是当前应用非常广泛的网络请求框架,通常结合RxJava来进行网络请求,本文将展示一个采用RxJava+Retrofit的网络请求demo. 集成步骤 1.app工程的build.gradle中添加依赖 //retrofit2 implementation 'com.google.code.gson:gson:2.8.5' implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.

随机推荐