1.依赖
3.Apiserver
4.main函数
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
2.Api
public class API {
public static final String PAGE_URL = "https://api.tianapi.com/wxnew/";
}
3.Apiserver
public interface ApiService {
@POST
Observable<PageBean> postpage(@Url String url, @QueryMap Map<String,String> map);
}
4.main函数
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postHome(API.PAGE_URL,"2","10");
}
public void postHome(String url,String page,String num){
Map<String,String> map=new HashMap<>();
map.put("key","8d6e3228d25298f13af4fc40ce6c9679");
map.put("num",num);
map.put("page",page);
Retrofit retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
ApiService apiService = retrofit.create(ApiService.class);
Observable<PageBean> postpages = apiService.postpage(url,map);
postpages.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<PageBean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(PageBean pageBean) {
Toast.makeText(MainActivity.this, ""+pageBean.getNewslist().get(1).getTitle().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}