版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a243845305/article/details/51211597
retrofit2.0使用教程:http://blog.csdn.net/angcyo/article/details/50351247
说实话,看了那么多教程也一直没有理解,在最后一个连接处结合了github上的一个retrofit2.0例子后才刚刚明白了一些。
说一下最开始的疑惑点,一直没弄懂各个博客上的代码一段一段的不知道该写在哪里,对于整个项目也没有一个概念,有些盲人摸象的滋味。很是不舒服啊,而且找的Demo也比较乱,来来回回弄了2天也没学会。不吐槽了,发出代码来最清楚。之后我会把demo的github地址贴出来。
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//在需要的地方拿到接口对象,service就是一个接口对象
GetBaiduClient.GetBaiduInterface service = GetBaiduClient.getBaiduInterface();
//Call<T>就是一个回调方法,获取到Call,我们才能执行请求
Call<ResponseBody> call = service.get();
//使用异步方式执行请求,关于同步异步可以在网上找一些博客
call.enqueue(new Callback<ResponseBody>() {//异步请求
@Override
public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
try {
//并没有使用Gson,只是获取一下请求,尝试一下retrofit怎么用,所以就直接Log输出了
String body = response.body().string();//获取返回体的字符串
Log.e("MainActivity", body);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
Log.e("MainActivity", "onFailure");
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//在需要的地方拿到接口对象,service就是一个接口对象
GetBaiduClient.GetBaiduInterface service = GetBaiduClient.getBaiduInterface();
//Call<T>就是一个回调方法,获取到Call,我们才能执行请求
Call<ResponseBody> call = service.get();
//使用异步方式执行请求,关于同步异步可以在网上找一些博客
call.enqueue(new Callback<ResponseBody>() {//异步请求
@Override
public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
try {
//并没有使用Gson,只是获取一下请求,尝试一下retrofit怎么用,所以就直接Log输出了
String body = response.body().string();//获取返回体的字符串
Log.e("MainActivity", body);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
Log.e("MainActivity", "onFailure");
}
});
}
}
GetBaiduClient
public class GetBaiduClient {
public static GetBaiduInterface getBaiduInterface() {
//在这里定义retrofit对象,一会接口要通过retrofit对象来创建
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.baidu.com/")
.build();
// 接口对象由retrofit对象创建而来
GetBaiduInterface getBaiduInterface = retrofit.create(GetBaiduInterface.class);
return getBaiduInterface;
}
public interface GetBaiduInterface {
//这里定义的方法都是一些将要与服务器进行交互的方法,在这里我只定义了一个get方法。大家可以自己多试试别的方法。
@GET("http://www.baidu.com/")
Call<ResponseBody> get();
public static GetBaiduInterface getBaiduInterface() {
//在这里定义retrofit对象,一会接口要通过retrofit对象来创建
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.baidu.com/")
.build();
// 接口对象由retrofit对象创建而来
GetBaiduInterface getBaiduInterface = retrofit.create(GetBaiduInterface.class);
return getBaiduInterface;
}
public interface GetBaiduInterface {
//这里定义的方法都是一些将要与服务器进行交互的方法,在这里我只定义了一个get方法。大家可以自己多试试别的方法。
@GET("http://www.baidu.com/")
Call<ResponseBody> get();
}
}
上面就是一个非常非常简单的Demo了,当然它也非常的不完善,很多retrofit2.0的内容以及使用方式都还不是很好,应该是要持续更新学习,后面附上两个demo的连接,一个是我的gitHub,还有一个是当时对比学习时的。大家可以下载来看一下。