您的位置:首页 > 其它

Retrofit(一)简单实用get方式

2017-10-30 15:55 148 查看
接触到一种新的网络请求框架,是Square公司贡献出来的,同时还有一些主流的网络与图片的框架(okhttp\Picass)。下面就简单的说一下Retrofit.


首先 我们应该导入第三方的依赖

compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'


很简单,接下来我们需要接口,利用定义一个网络接口 ,里面进行封装请求方式和请求参数。看代码

/**
* 无参get请求
* http://service.meiyinkeqiu.com/service/ads/cptj *
* @return
*/

@GET("service/ads/cptj")
Call<News>  getData();
/**
* 有参get请求
* 拼接参数 /形式
*
* @return https://api.github.com/users/baiiu */
@GET("users/{user}")
Call<User>  getArgumentData(@Path("user") String user);
/**
* http://www.93.gov.cn/93app/data.do * channelId
* startNum
* 拼接 ? &
* 为主
*/
@GET("data.do")
Call<Info>  getArgumentData2(@Query("channelId") int channelId, @Query("startNum") int startNum);


这里只是简单的get请求方式,一共有三种请求地址,一般用到的就是第二三种,第三种是我们用到最多

这里注意一下,我们需要在Call的泛型添加其对应的对象

最后就需要主activity里面进行具体的操作方法,先看代码然后进行解释

ublic class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取数据
// getData();
//getArgumentData();
getArgumentData2();
}

----------

private void getArgumentData2() {
//得到Retrofit对象 建造者设计模式
Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.baseUrl3).addConverterFactory(GsonConverterFactory.create()).build();
//得到网络接口 通过动态代理的方式获得
RetrofitService retrofitService = retrofit.create(RetrofitService.class);
Call<Info> call = retrofitService.getArgumentData2(1, 0);
call.enqueue(new Callback<Info>() {
@Override
public void onResponse(Call<Info> call, Response<Info> response) {
Info info = response.body();
List<Info.DataInfo> data = info.data;
for (Info.DataInfo infos:data ) {
Toast.makeText(MainActivity.this, "请求成功:"+infos.TITLE, Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Call<Info> call, Throwable t) {

}
});

}

----------

private void getArgumentData() {
//得到Retrofit对象 建造者设计模式
Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.baseUrl2).addConverterFactory(GsonConverterFactory.create()).build();
//得到网络接口 通过动态代理的方式获得
RetrofitService retrofitService = retrofit.create(RetrofitService.class);
Call<User> call = retrofitService.getArgumentData("forever");
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
User user = response.body();
Toast.makeText(MainActivity.this, "请求成功:"+user.avatar_url, Toast.LENGTH_SHORT).show();
}

@Override
public void onFailure(Call<User> call, Throwable t) {

}
});

}

----------

private void getData() {
//得到Retrofit对象 建造者设计模式
Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.baseUrl1)
.addConverterFactory(GsonConverterFactory.create()).build();
//得到网络接口 通过动态代理的方式获得
RetrofitService retrofitService = retrofit.create(RetrofitService.class);
//获得Call
Call<News> call = retrofitService.getData();
//执行异步请求
call.enqueue(new Callback<News>() {
@Override
public void onResponse(Call<News> call, Response<News> response) {
//回调在主线程
News news = response.body();
List<News.Ads> ads = news.ads;
for (int i = 0; i < ads.size(); i++) {
News.Ads ads1 = ads.get(i);
String imgsrc = ads1.imgsrc;
String gonggaoren = ads1.gonggaoren;
Toast.makeText(MainActivity.this, "请求成功:"+imgsrc+","+gonggaoren, Toast.LENGTH_SHORT).show();
}

}

@Override
public void onFailure(Call<News> call, Throwable t) {

}
});

}
}


我们先得到retrofit的对象,我们通过建造者模式,将相对应的参数进行添加(请求地址 ..gson..),

Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.baseUrl3).addConverterFactory(GsonConverterFactory.create()).build();


然后得到网络接口 通过动态代理的方式获得

RetrofitService retrofitService = retrofit.create(RetrofitService.class);


然后我们调用网络接口里面的方法,将请求参数传递进去,通过异步请求方式最后得到请求结果的回调的方法。

Call<Info> call = retrofitService.getArgumentData2(1, 0);
call.enqueue(new Callback<Info>() {
@Override
public void onResponse(Call<Info> call, Response<Info> response) {
Info info = response.body();
List<Info.DataInfo> data = info.data;
for (Info.DataInfo infos:data ) {
Toast.makeText(MainActivity.this, "请求成功:"+infos.TITLE, Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Call<Info> call, Throwable t) {

}
});


这里首先注意的就是成功或者失败的回调方法是在主线程里面,我们可以直接进行更新UI的操作

好了,本次内容就到这里,以后还会继续更新,敬请期待!!!!!!!!!!!!!!!!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: