作者:Valar_Ray
如需转载请保留原文链接
IDE:Android Studio
Retrofit版本:2.3.0
Gson版本:2.8.1
retrofit的官方文档:http://square.github.io/retrofit/
retrofit gitHub项目地址:https://github.com/square/retrofit
gson gitHub项目地址:https://github.com/google/gson
本文目录:
- Retrofit与Gson包的引入
- 实体对象的创建
- 接口定义
- Retrofit对象的创建与实现
- 结论(踩过的坑)
1.Retrofit与Gson包的引入
在Android项目的build.gradle中的添加
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
2.实体对象的创建
先看一下要解析的Json数据:
{"total": 381,"per_page": 8,"current_page": 1,"last_page": 48,
"data": [{"id": 1,"title": "南极人冰丝席凉席三件套1.8m床可折叠夏季凉席1.5宿舍单人席子1.2"},
{"id": 2,"title": "夏季背心"}]
}
根据Json数据创建的实体对象如下
首先是Goods.class:
public class Goods{
public int total;
public int per_page;
public List<Good> data;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPer_page() {
return per_page;
}
public void setPer_page(int per_page) {
this.per_page = per_page;
}
public List<Good> getData() {
return data;
}
public void setData(List<Good> data) {
this.data = data;
}
@Override
public String toString() { //重写toString方法,便于观察后来的解析结果
return "Goods{" +
"total=" + total +
", per_page=" + per_page +
", data=" + data +
'}';
}
}
然后是Good.class:
public class Good {
public int id;
public String title;
@Override
public String toString() {
return "Good{" +
"id=" + id +
", title='" + title + '\'' +
'}';
}
}
3.接口定义
import retrofit2.Call;
import retrofit2.http.GET;
public interface GitHubService {
@GET("api/test") \\其中写的是Url中主机名后面的地址
Call<Goods> getGood();
}
4.Retrofit对象的创建与实现
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.16.125/") //要访问的主机地址,注意以 /(斜线) 结束,不然可能会抛出异常
.addConverterFactory(GsonConverterFactory.create()) //添加Gson
.build();
GitHubService service = retrofit.create(GitHubService.class);
Call<Goods> call = service.getGood();
call.enqueue(new Callback<Goods>() {
@Override
public void onResponse(Call<Goods> call, Response<Goods> response) {
Goods goods = response.body();
Log.d("sxl", goods != null ? goods.toString() :"null");
}
@Override
public void onFailure(Call<Goods> call, Throwable t) {
t.printStackTrace();
}
});
}
}
Log打印出的结果如下:
D/sxl: Goods{total=381, per_page=8, data=[Good{id=1, title='南极人冰丝席凉席三件套1.8m床可折叠夏季凉席1.5宿舍单人席子1.2'}, Good{id=2, title='........
5.结论(踩过的坑)
- 注意先开启App的网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
- 别忘了导入 com.squareup.retrofit2:converter-gson:2.3.0
本文参考:http://square.github.io/retrofit/
http://www.jianshu.com/p/308f3c54abdd
http://blog.csdn.net/u012301841/article/details/49685677