欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Retrofit和Rxjava的网络请求

程序员文章站 2023-11-24 14:25:34
android  retrofit和rxjava的网络请求 去年的时候好多公司就已经使用rxjava和retrofit了,最近自自己学习了一下,感觉真的是很好用...

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>

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