Android开源框架——Retrofit入门(二)
来源:互联网 发布:广州米多网络副总 编辑:程序博客网 时间:2024/05/29 12:17
知乎日报的开放API: http://news-at.zhihu.com/api/4/news/latest
这个API返回的是json字符串,我们可以通过一些方式将json字符串转化为Java实体类。
1、推荐使用网上的一个服务 http://www.bejson.com/json2javapojo/
2、AndroidStudio也提供了一个插件GsonFormat,大家可以自行查询,将Json转化为实体Java类。
例子: 通过使用Retrofit来获取知乎日报
添加依赖
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'//Retrofit2所需要的包compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'//ConverterFactory的Gson依赖包
添加网络许可
<uses-permission android:name="android.permission.INTERNET" />
1>通过知乎日报API返回的数据来获取一个实体类ZhihuEntity,相关代码如下:
package com.example.app_retrofit;import java.util.List;/** * Created by 尽途 on 2017/7/21. */public class ZhihuEntity { /** * date : 20170721 * stories : [{"images":["https://pic4.zhimg.com/v2-81bcb9402f5de2bc332792815d891feb.jpg"],"type":0,"id":9535059,"ga_prefix":"072112","title":"大误 · 光绪 20 年,我的左臂没了"},{"images":["https://pic4.zhimg.com/v2-331fddc6b65a7c1010201a94bfa4f95b.jpg"],"type":0,"id":9527991,"ga_prefix":"072111","title":"近几年受追捧的日式建筑风格,其实也不完美"},{"images":["https://pic3.zhimg.com/v2-691cddd7bc8293d4ffb5af3d306c687a.jpg"],"type":0,"id":9533795,"ga_prefix":"072111","title":"不要长篇大论,就推荐 3 个最实用的 Excel 函数"},{"images":["https://pic3.zhimg.com/v2-abd6e944219c5722ec319bd40fc206a2.jpg"],"type":0,"id":9532340,"ga_prefix":"072109","title":"火过之后,VR 为何没成为像 iPhone 一样的时尚流行品?"},{"images":["https://pic1.zhimg.com/v2-1a7914acd259139728d77562f37e1bf4.jpg"],"type":0,"id":9534199,"ga_prefix":"072108","title":"家长帮助孩子的方法,就像是建筑用的「脚手架」"},{"title":"买房时说得多好,入住以后绿化没了,电梯坏了,遍地垃圾","ga_prefix":"072107","images":["https://pic3.zhimg.com/v2-f04ae40601d33409e15c1b8725d6814e.jpg"],"multipic":true,"type":0,"id":9531776},{"images":["https://pic4.zhimg.com/v2-ee46569b08751e54cd37a632e68128bf.jpg"],"type":0,"id":9533680,"ga_prefix":"072107","title":"比统计局早半个月预测出经济趋势,真是厉害"},{"images":["https://pic2.zhimg.com/v2-3dbcf22093c7ef491c4420d106064c25.jpg"],"type":0,"id":9534297,"ga_prefix":"072107","title":"怎么成为老司机?来一条一条对照着练吧"},{"images":["https://pic2.zhimg.com/v2-acb5bec97d166312b220b684b6f0d689.jpg"],"type":0,"id":9532612,"ga_prefix":"072106","title":"瞎扯 · 如何正确地吐槽"},{"images":["https://pic2.zhimg.com/v2-0b63ecae67fb62a9ac8728acd36acc0d.jpg"],"type":0,"id":9533500,"ga_prefix":"072106","title":"这里是广告 · 一包未来定制牛奶里能有多少高科技?"}] * top_stories : [{"image":"https://pic4.zhimg.com/v2-91e5099dc6a7ebb7e865221a050e3eaf.jpg","type":0,"id":9534297,"ga_prefix":"072107","title":"怎么成为老司机?来一条一条对照着练吧"},{"image":"https://pic1.zhimg.com/v2-74230046934f7defbe40aa04f11078e4.jpg","type":0,"id":9533680,"ga_prefix":"072107","title":"比统计局早半个月预测出经济趋势,真是厉害"},{"image":"https://pic3.zhimg.com/v2-0a1983daeb6b6b4fe1c8b8ce79f266b6.jpg","type":0,"id":9531776,"ga_prefix":"072107","title":"买房时说得多好,入住以后绿化没了,电梯坏了,遍地垃圾"},{"image":"https://pic4.zhimg.com/v2-3f3cdb304fa86088fb31a01dbc940013.jpg","type":0,"id":9533191,"ga_prefix":"072019","title":"是不是觉得自己拍的照片挺美,就是看起来不高级?"},{"image":"https://pic4.zhimg.com/v2-614dffa0ed36c5e13f8e34a486922f3f.jpg","type":0,"id":9526772,"ga_prefix":"072016","title":"一届奥斯卡,三部片子剧情撞车:动画史上最大的抄袭悬案"}] */ private String date; private List<StoriesBean> stories; private List<TopStoriesBean> top_stories; public String getDate() { return date; } public void setDate(String date) { this.date = date; } public List<StoriesBean> getStories() { return stories; } public void setStories(List<StoriesBean> stories) { this.stories = stories; } public List<TopStoriesBean> getTop_stories() { return top_stories; } public void setTop_stories(List<TopStoriesBean> top_stories) { this.top_stories = top_stories; } public static class StoriesBean { /** * images : ["https://pic4.zhimg.com/v2-81bcb9402f5de2bc332792815d891feb.jpg"] * type : 0 * id : 9535059 * ga_prefix : 072112 * title : 大误 · 光绪 20 年,我的左臂没了 * multipic : true */ private int type; private int id; private String ga_prefix; private String title; private boolean multipic; private List<String> images; public int getType() { return type; } public void setType(int type) { this.type = type; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGa_prefix() { return ga_prefix; } public void setGa_prefix(String ga_prefix) { this.ga_prefix = ga_prefix; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public boolean isMultipic() { return multipic; } public void setMultipic(boolean multipic) { this.multipic = multipic; } public List<String> getImages() { return images; } public void setImages(List<String> images) { this.images = images; } } public static class TopStoriesBean { /** * image : https://pic4.zhimg.com/v2-91e5099dc6a7ebb7e865221a050e3eaf.jpg * type : 0 * id : 9534297 * ga_prefix : 072107 * title : 怎么成为老司机?来一条一条对照着练吧 */ private String image; private int type; private int id; private String ga_prefix; private String title; public String getImage() { return image; } public void setImage(String image) { this.image = image; } public int getType() { return type; } public void setType(int type) { this.type = type; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGa_prefix() { return ga_prefix; } public void setGa_prefix(String ga_prefix) { this.ga_prefix = ga_prefix; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }}
2>接口定义(以知乎日报API为例)接口名定义为GetZhihu1,具体如下:
public interface GetZhihu1 { @GET("/api/4/news/latest") Call<ZhihuEntity>getZhihuNews();
3>创建Retrofit并引用
Retrofit retrofit=new Retrofit .Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create())//涉及到json到实体的转变, .build(); GetZhihu1 getZhihu1=retrofit.create(GetZhihu1.class); Call<ZhihuEntity>call=getZhihu1.getZhihuNews(); call.enqueue(new Callback<ZhihuEntity>() { @Override public void onResponse(Call<ZhihuEntity> call, Response<ZhihuEntity> response) { List<ZhihuEntity.StoriesBean> storiesBeen=response.body().getStories();//获取 text_zhihu_get.setText(storiesBeen.get(1).getTitle());//x显示一部分 } @Override public void onFailure(Call<ZhihuEntity> call, Throwable t) { } });
具体代码如下:
1、XML代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <Button android:id="@+id/button_zhihu1_get" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取"/> <TextView android:id="@+id/text_zhihu1_get" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world"/></LinearLayout>2、Java代码:
package com.example.app_retrofit;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.Button;import android.widget.TextView;import java.util.List;import butterknife.Bind;import butterknife.ButterKnife;import butterknife.OnClick;import retrofit2.Call;import retrofit2.Callback;import retrofit2.Response;import retrofit2.Retrofit;import retrofit2.converter.gson.GsonConverterFactory;public class Zhihu1Activity extends AppCompatActivity { @Bind(R.id.button_zhihu1_get) Button button_zhihu1_get; @Bind(R.id.text_zhihu1_get) TextView text_zhihu_get; private String baseUrl="http://news-at.zhihu.com"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zhihu1); ButterKnife.bind(this); } @OnClick(R.id.button_zhihu1_get) public void onclickButton_zhihi1_get(){ Retrofit retrofit=new Retrofit .Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create())//涉及到json到实体的转变, .build(); GetZhihu1 getZhihu1=retrofit.create(GetZhihu1.class); Call<ZhihuEntity>call=getZhihu1.getZhihuNews(); call.enqueue(new Callback<ZhihuEntity>() { @Override public void onResponse(Call<ZhihuEntity> call, Response<ZhihuEntity> response) { List<ZhihuEntity.StoriesBean> storiesBeen=response.body().getStories(); text_zhihu_get.setText(storiesBeen.get(1).getTitle()); } @Override public void onFailure(Call<ZhihuEntity> call, Throwable t) { } }); }}3、接口代码:
package com.example.app_retrofit;import retrofit2.Call;import retrofit2.http.GET;/** * Created by 尽途 on 2017/7/29. */public interface GetZhihu1 { @GET("/api/4/news/latest") Call<ZhihuEntity>getZhihuNews();}4、实现结果与问题:
代码片段中的ButterKnife为一个挺好用的view注入框架,在此不再赘述。
阅读全文
0 0
- Android开源框架——Retrofit入门(二)
- Android开源框架——Retrofit入门(一)
- Android开源网络框架Retrofit(入门篇)
- Android网络开发框架Retrofit(二:入门篇,hello, world)
- Retrofit框架(入门级)
- Retrofit框架学习(二)
- Android 网络访问框架——Retrofit
- Android网络请求框架:Retrofit入门
- android网络框架retrofit源码解析二
- Android网络请求框架之Retrofit(二)
- android retrofit 示例(二)
- Retrofit网络请求框架使用简析——Android网络请求框架(四)
- 深入解析开源项目之Retrofit(二)框架篇
- retrofit框架探究(二)
- android 开源框架 Retrofit的简单使用
- android网络框架Retrofit——使用简介
- Android网络篇—Retrofit网络请求框架详解
- Android入门学习——Retrofit+MVP模式学习
- hdu2081 手机短号(C语言)
- csuoj1972大梵天的恩赐
- Qt中常用的快捷键
- Java基础之方法
- WUST OJ 1497: yj&&jy->Cake
- Android开源框架——Retrofit入门(二)
- python opencv入门 图像梯度(14)
- Velocity中#parse与#include区别
- Java后台进阶-指导
- 问题:请用四条连接的直线将规则排列(三行三列)的九个点连起来。
- nginx上配置反向代理到tomcat
- Django 中间件的研究
- 测体脂率 脂肪率
- 怎样将矩阵Reshape