美文网首页
Retrofit简单使用

Retrofit简单使用

作者: 随_26d5 | 来源:发表于2018-08-20 16:15 被阅读0次

/* 第一个接口:

http://39.107.224.233/firstga/app/news/listNewsChannel

请求头:

Content-Type:application/x-www-form-urlencoded

* */

ApiService接口

@Headers("Content-Type:application/x-www-form-urlencoded")

@POST("listNewsChannel")

Observable getBean1();

Retrofit retrofit1 =new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

.baseUrl("http://39.107.224.233/firstga/app/news/").build();

retrofit1.create(ApiService.class).getBean1().subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber() {

@Override

    public void onCompleted() {

}

@Override

    public void onError(Throwable e) {

}

@Override

    public void onNext(Bean1 bean1) {

tv.setText("btn1" + bean1.getData().getNewsChannelList().get(0).getChannelName());

}

});

/** 第二个接口:

http://39.107.224.233/firstga/app/news/downListNews

参数:

{"userId":"aadc2d9fe76f4b89bf37ca738e23eafe","channelId":"b9240eee3b0211e8b64c00163e30445d","cursor": 0}

userid  当前为固定值,不用改

channelId  这个是第一个接口中返回的数据(如果为咨询,默认为0 )

cursor  这个是默认值0

求请头:

Content-Type:application/json

* */

@Headers("Content-Type:application/json")

@POST("downListNews")

Observable getBean2(@Body RequestBody body);

Retrofit retrofit2 =new Retrofit.Builder()

.addConverterFactory(GsonConverterFactory.create())

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

.baseUrl("http://39.107.224.233/firstga/app/news/").build();

//{"userId":"aadc2d9fe76f4b89bf37ca738e23eafe","channelId":"b9240eee3b0211e8b64c00163e30445d","cursor": 0}

MediaType mediaType = MediaType.parse("Content-Type:application/json");

RequestBody body = RequestBody.create(mediaType,

"{\"userId\":\"aadc2d9fe76f4b89bf37ca738e23eafe\",\"channelId\":\"b9240eee3b0211e8b64c00163e30445d\",\"cursor\": 0}");

ApiService apiService = retrofit2.create(ApiService.class);

apiService

.getBean2(body)

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(new Subscriber() {

@Override

            public void onCompleted() {

}

@Override

            public void onError(Throwable e) {

Log.e("ssssssssssssssss", e.getMessage());

}

@Override

            public void onNext(Bean2 bean2) {

tv.setText("btn2" + bean2.getMessage());

}

});

/*第三个接口

* https://api.yunxuekeji.cn/yunxue_app_api/course/getCourseByTypeAndMore?orderOn=&

* classtype=031001004&forPeopleType=&format=&price=&pageIndex=1&pageSize=11&classTag=

* */

@FormUrlEncoded

@POST("getCourseByTypeAndMore")

Observable getBean3(@FieldMap Map map);

Retrofit retrofit3 =new Retrofit.Builder()

.addConverterFactory(GsonConverterFactory.create())

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

.baseUrl("https://api.yunxuekeji.cn/yunxue_app_api/course/")

.build();

ApiService apiService3 = retrofit3.create(ApiService.class);

HashMap map =new HashMap<>();

map.put("orderOn","");

map.put("classtype","031001004");

map.put("forPeopleType","");

map.put("format","");

map.put("price","");

map.put("pageIndex","1");

map.put("pageSize","11");

map.put("classTag","");

apiService3.getBean3(map).subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(new Subscriber() {

@Override

            public void onCompleted() {

}

@Override

            public void onError(Throwable e) {

}

@Override

            public void onNext(Bean3 bean3) {

data = bean3.getBody().getResult().getData();

tv.setText("btn3"+bean3.getBody().getResult().getData().get(0).getClassName()+bean3.getBody().getResult().getData().get(0).getID());

}

});

相关文章

网友评论

      本文标题:Retrofit简单使用

      本文链接:https://www.haomeiwen.com/subject/uoiqsftx.html