- OkHttp是一个成熟的网络通信方案,实现了几乎和Java.net.HttpURLConnection一样的API。
- Gson是Google开发的解析Json的工具。
- GsonFormat是能够快速实现JavaBean的AndroidStudio插件。GsonFormat安装方法如下:
-
- File->Settings..->Plugins–>Browse repositores->搜索GsonFormat
- 2.安装插件,重启android studio
以 http://gank.io/api/data/福利/6/1 为例子,这个组合的基本使用流程如下:
1,利用Gsonformat生成GirlPageBean,用于存储从网上拉下来的数据
在Android Studio中点击Code->Generate->GsonFormat,把http://gank.io/api/data/福利/6/1
的json数据拷贝到Gsonformat中,利用Gsonformat生成bean类。以下为自动生成的bean。
package com.neusnow.ganksimple.bean;
import java.util.List;
/**
* Created by xujin on 16/07/28.
*/
public class GirlPageBean {
/**
* error : false
* results : [{"_id":"57995869421aa90d43bbf042","createdAt":"2016-07-28T08:57:13.293Z","desc":"葛优躺","publishedAt":"2016-07-28T18:17:20.567Z","source":"chrome","type":"福利","url":"http://ww3.sinaimg.cn/large/610dc034jw1f69c9e22xjj20u011hjuu.jpg","used":true,"who":"代码家"},{"_id":"57981ee6421aa90d36e96090","createdAt":"2016-07-27T10:39:34.818Z","desc":"王子文","publishedAt":"2016-07-27T11:27:16.610Z","source":"chrome","type":"福利","url":"http://ww3.sinaimg.cn/large/610dc034jw1f689lmaf7qj20u00u00v7.jpg","used":true,"who":"代码家"},{"_id":"5796b970421aa90d2fc94b4e","createdAt":"2016-07-26T09:14:24.76Z","desc":"今天两个妹子","publishedAt":"2016-07-26T10:30:11.357Z","source":"chrome","type":"福利","url":"http://ww3.sinaimg.cn/large/c85e4a5cjw1f671i8gt1rj20vy0vydsz.jpg","used":true,"who":"代码家"},{"_id":"5794df0e421aa90d39e70939","createdAt":"2016-07-24T23:30:22.399Z","desc":"7.25","publishedAt":"2016-07-25T11:43:57.769Z","source":"chrome","type":"福利","url":"http://ww2.sinaimg.cn/large/610dc034jw1f65f0oqodoj20qo0hntc9.jpg","used":true,"who":"代码家"},{"_id":"57918b5c421aa90d2fc94b35","createdAt":"2016-07-22T10:56:28.274Z","desc":"恐龙爪子萌妹子","publishedAt":"2016-07-22T11:04:44.305Z","source":"web","type":"福利","url":"http://ww2.sinaimg.cn/large/c85e4a5cgw1f62hzfvzwwj20hs0qogpo.jpg","used":true,"who":"代码家"},{"_id":"578f93c4421aa90de83c1bf4","createdAt":"2016-07-20T23:07:48.480Z","desc":"7.21","publishedAt":"2016-07-20T16:09:07.721Z","source":"chrome","type":"福利","url":"http://ww4.sinaimg.cn/large/610dc034jw1f60rw11f5mj20iy0sg0u2.jpg","used":true,"who":"daimajia"},{"_id":"578e3d3b421aa90df33fe7e8","createdAt":"2016-07-19T22:46:19.966Z","desc":"7.20","publishedAt":"2016-07-20T17:25:17.522Z","source":"chrome","type":"福利","url":"http://ww4.sinaimg.cn/large/610dc034jw1f5zlndsr0oj20go0ciq4h.jpg","used":true,"who":"代码家"},{"_id":"578da129421aa90dea11ea28","createdAt":"2016-07-19T11:40:25.857Z","desc":"7.19","publishedAt":"2016-07-19T12:13:10.925Z","source":"chrome","type":"福利","url":"http://ww2.sinaimg.cn/large/610dc034jw1f5z2eko5xnj20f00miq5s.jpg","used":true,"who":"代码家"},{"_id":"578c4ecf421aa90dea11ea17","createdAt":"2016-07-18T11:36:47.363Z","desc":"7.18","publishedAt":"2016-07-18T11:49:19.322Z","source":"chrome","type":"福利","url":"http://ww4.sinaimg.cn/large/610dc034jw1f5xwnxj2vmj20dw0dwjsc.jpg","used":true,"who":"代码家"},{"_id":"57885b8c421aa90de83c1b96","createdAt":"2016-07-15T11:42:04.295Z","desc":"7.15","publishedAt":"2016-07-15T11:56:07.907Z","source":"chrome","type":"福利","url":"http://ww4.sinaimg.cn/large/610dc034jw1f5ufyzg2ajj20ck0kuq5e.jpg","used":true,"who":"代码家"}]
*/
private boolean error;
/**
* _id : 57995869421aa90d43bbf042
* createdAt : 2016-07-28T08:57:13.293Z
* desc : 葛优躺
* publishedAt : 2016-07-28T18:17:20.567Z
* source : chrome
* type : 福利
* url : http://ww3.sinaimg.cn/large/610dc034jw1f69c9e22xjj20u011hjuu.jpg
* used : true
* who : 代码家
*/
private List results;
public boolean isError() {
return error;
}
public List getResults() {
return results;
}
public static class ResultsBean {
private String _id;
private String createdAt;
private String desc;
private String publishedAt;
private String source;
private String type;
private String url;
private boolean used;
private String who;
public String get_id() {
return _id;
}
public String getCreatedAt() {
return createdAt;
}
public String getDesc() {
return desc;
}
public String getPublishedAt() {
return publishedAt;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getType() {
return type;
}
public String getUrl() {
return url;
}
public boolean isUsed() {
return used;
}
public String getWho() {
return who;
}
}
}
2,在主程序中创建 okHttpClient对象
public class MainActivity extends AppCompatActivity {
.....
public OkHttpClient mOkHttpClient;
protected void onCreate(Bundle savedInstanceState) {
......
//1. 创建okHttpClient对象
mOkHttpClient = new OkHttpClient();
initData();
}
public void initData(){
handler.postDelayed(new Runnable() {
@Override
public void run() {
getData();
}
}, 100);
}
3, 在子线程中获得数据
private void getData(int index) {
//1. 获取Request对象
Request request = new Request.Builder()
.url("http://gank.io/api/data/福利/6/1")
.build();
//通过request的对象去构造得到一个Call对象,请求加入调度
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
String res = response.body().string();
Log.v("te::","download complete...1");
Collection girls = analysisJson(res);//分析回应的json数据
data.addAll(girls);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
mSwipeRefreshLayout.setRefreshing(false);
adapter.notifyItemRemoved(adapter.getItemCount());
Toast.makeText(MainActivity.this, "下载完成," + adapter.getItemCount(), Toast.LENGTH_SHORT).show();
}
});
}
}
3,利用GSON解析JSON
/**
* 利用GSON解析JSON
*/
private Collection analysisJson(String jsonData){
Collection girls = new ArrayList();
Gson gson = new Gson();
GirlPageBean girlPageBean = gson.fromJson(jsonData, GirlPageBean.class);
List results = girlPageBean.getResults();
for(GirlPageBean.ResultsBean res : results){
Girl girl = new Girl();
girl.setDesc(res.getDesc());
girl.setUrl(res.getUrl());
girl.setPublishedAt(res.getPublishedAt());
girl.setWho(res.getWho());
girls.add(girl);
}
return girls;
}
- 作为例子的Demo
https://github.com/GinSmile/GankSimple/tree/5d43d9224a88ffe68ff59913af1062c79b1c0da4