简单使用了Butterknife+Retrofit

库配置
Project级的build.gradle

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

App级的build.gradle

apply plugin: 'com.android.application'
apply plugin:'android-apt'android {compileSdkVersion 23buildToolsVersion "23.0.3"defaultConfig {applicationId "com.android.retrofitdemo"minSdkVersion 9targetSdkVersion 23versionCode 1versionName "1.0"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}
}dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])testCompile 'junit:junit:4.12'compile 'com.android.support:appcompat-v7:23.4.0'compile 'com.squareup.retrofit2:converter-gson:2.1.0'compile 'com.squareup.picasso:picasso:2.5.2'compile 'com.jakewharton:butterknife:8.2.1'apt 'com.jakewharton:butterknife-compiler:8.2.1'
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ListView
        android:id="@+id/news_listView"android:layout_width="match_parent"android:layout_height="match_parent"></ListView></LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><ImageView
        android:id="@+id/news_imageView"android:layout_width="70dp"android:layout_height="70dp" /><LinearLayout
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextView
            android:id="@+id/news_title_textView"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextView
            android:id="@+id/news_description_textView"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout></LinearLayout>

实体类

package com.android.retrofitdemo;import com.google.gson.annotations.SerializedName;/*** 新闻数据*/public class News {@SerializedName("id")private int id;@SerializedName("name")private String name;//名称@SerializedName("food")private String  food;//食物@SerializedName("img")private String img;//图片@SerializedName("images")private String images;//图片,@SerializedName("description")private String description;//描述@SerializedName("keywords")private String keywords;//关键字@SerializedName("message")private String message;//资讯内容@SerializedName("count")private int count ;//访问次数@SerializedName("fcount")private int fcount;//收藏数@SerializedName("rcount")private int rcount;//评论读数public int getCount() {return count;}public void setCount(int count) {this.count = count;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public int getFcount() {return fcount;}public void setFcount(int fcount) {this.fcount = fcount;}public String getFood() {return food;}public void setFood(String food) {this.food = food;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getImages() {return images;}public void setImages(String images) {this.images = images;}public String getImg() {return img;}public void setImg(String img) {this.img = img;}public String getKeywords() {return keywords;}public void setKeywords(String keywords) {this.keywords = keywords;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getRcount() {return rcount;}public void setRcount(int rcount) {this.rcount = rcount;}
}
package com.android.retrofitdemo;import com.google.gson.annotations.SerializedName;import java.util.List;public class Tngou {@SerializedName("status")private boolean status;@SerializedName("total")private long total;@SerializedName("tngou")private List<News> newsList;public List<News> getNewsList() {return newsList;}public void setNewsList(List<News> newsList) {this.newsList = newsList;}public boolean isStatus() {return status;}public void setStatus(boolean status) {this.status = status;}public long getTotal() {return total;}public void setTotal(long total) {this.total = total;}
}

数据适配器

package com.android.retrofitdemo;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;import com.squareup.picasso.Picasso;import java.util.Collection;
import java.util.List;/*** 新闻列表适配器*/
public class NewsAdapter extends BaseAdapter{private Context mContext;private List<News> mNewsList;public NewsAdapter(Context context, List<News> newsList){mContext = context;mNewsList = newsList;}@Overridepublic int getCount() {return mNewsList.size();}@Overridepublic Object getItem(int position) {return mNewsList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder;if(convertView == null){convertView = LayoutInflater.from(mContext).inflate(R.layout.news_item,null);viewHolder = new ViewHolder();viewHolder.newsImageView = (ImageView) convertView.findViewById(R.id.news_imageView);viewHolder.newsTitleTextView = (TextView) convertView.findViewById(R.id.news_title_textView);viewHolder.newsDescriptionTextView = (TextView) convertView.findViewById(R.id.news_description_textView);convertView.setTag(viewHolder);}viewHolder = (ViewHolder) convertView.getTag();Picasso.with(mContext).load("http://tnfs.tngou.net/image"+mNewsList.get(position).getImg()).into(viewHolder.newsImageView);viewHolder.newsTitleTextView.setText(mNewsList.get(position).getName());viewHolder.newsDescriptionTextView.setText(mNewsList.get(position).getDescription());return convertView;}public final class ViewHolder{public ImageView newsImageView;public TextView newsTitleTextView;public TextView newsDescriptionTextView;}public void addAll(Collection<? extends News> collection){mNewsList.addAll(collection);notifyDataSetChanged();}}

网络请求接口

package com.android.retrofitdemo;import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;/*** 获取新闻列表*/
public interface NewsService {@GET("/api/cook/list" )Call<Tngou> getNews(@Query("id") int id, @Query("page") int page, @Query("rows") int rows);
}
package com.android.retrofitdemo;import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;import java.util.ArrayList;
import java.util.List;import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;public class MainActivity extends AppCompatActivity implements Callback<Tngou> {@BindView(R.id.news_listView)ListView newsListView;private NewsAdapter adapter;private List<News> newsList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButterKnife.bind(this);Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.tngou.net").addConverterFactory(GsonConverterFactory.create()).build();NewsService newsService = retrofit.create(NewsService.class);Call<Tngou> call = newsService.getNews(0,1,20);call.enqueue(this);ListView newsListView = (ListView) findViewById(R.id.news_listView);adapter = new NewsAdapter(this, new ArrayList<News>());newsListView.setAdapter(adapter);}@Overridepublic void onResponse(Call<Tngou> call, Response<Tngou> response) {newsList = response.body().getNewsList();adapter.addAll(newsList);}@Overridepublic void onFailure(Call<Tngou> call, Throwable t) {t.printStackTrace();}
}

Retrofit解析网页Json数据简单实例相关推荐

  1. oracle大对象实例_Oracle解析复杂json的方法实例详解

    问题背景: 当前在Oracle数据库(11G之前的版本)解析json没有可以直接使用的系统方法,网上流传的PLSQL脚本大多也只可以解析结构较单一的json串,对于结构复杂的json串还无法解析.如此 ...

  2. mormot解析天气预报JSON数据

    mormot解析天气预报JSON数据 uses SynCommons; const json2 = '{' + #13#10 + '"error":0,' + #13#10 + ' ...

  3. java connection 共享_java 使用HttpURLConnection发送数据简单实例

    java 使用HttpURLConnection发送数据简单实例 每个 HttpURLConnection 实例都可用于生成单个请求,但是其他实例可以透明地共享连接到 HTTP 服务器的基础网络.请求 ...

  4. python为什么closed_为什么Python无法解析此JSON数据? [关闭] - Why can't Python parse this JSON data? [closed]...

    问题: I have this JSON in a file: 我在文件中有此JSON: { "maps": [ { "id": "blabla&qu ...

  5. 组装复杂json请求数据和解析复杂json数据

    在项目中有很多的接口对接的问题,于是就遇到了客户提供的各种奇葩的请求体.说一下最近遇到的一个复杂的json格式请求数据如下: {"bizData": {"userList ...

  6. js解析\遍历json数据中所有的键和值

    js解析\遍历json数据中所有的键和值 for(var key in json){ console.log(key)    //键 consolelog(json[key])  //值 } 注:数组 ...

  7. Fastjson解析复杂json数据

    大体上分为三步,1.准备json数据.2,导入fastjson包.3,编写代码测试. 一.准备要解析的json数据:(够复杂了吧) {"code":200, "msg&q ...

  8. 在线解析xml,json数据的网址

    在线解析xml,json数据的网址 https://www.sojson.com/yasuoyihang.html

  9. 解析新浪微博JSON数据

    解析新浪微博JSON数据 这里讲的是通过retrofit2请求方式得到的返回值 一.自己分析解析 默认通过responseBody 将得到的返回值 try { str=response.body(). ...

最新文章

  1. [转]SIFT特征提取分析
  2. C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法...
  3. 数据结构(严蔚敏)之三——顺序栈之c语言实现
  4. !!从中位数市盈率看目前市场位置
  5. 计算机应用基础专科,2016电子科技大学计算机应用基础(专科)在线作业2
  6. java streams_使用Stream.peek在Java Streams内部进行窥视
  7. 数据结构【高精度专题】
  8. -Java基础-方法
  9. 清华霸榜,长沙理工异军突起!第三届 CCF CCSP落下帷幕
  10. H.266/VVC代码学习笔记15:VTM6.0中的xCheckRDCostMergeTriangle2Nx2N()函数
  11. usbcan、can分析仪的产品特点和功能特点
  12. 提升工作效率的一些工具
  13. 微信多订单合并付款_拼多多只能微信支付吗?拼多多合并支付有什么优势?
  14. 牛客网刷题java之变态跳台阶一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
  15. [Swust OJ 643]--行列式的计算(上三角行列式变换)
  16. 瓴羊CEO朋新宇:从数据发现问题到数据创造价值|2022全球数字价值峰会-阿里云开发者社区
  17. 关于debug时的断点无效问题 [已解决,不知原因]
  18. 简单发送QQ邮件教程
  19. Postgresql修改时区
  20. 学习博客:【JavaScript】jQuery

热门文章

  1. 第六天2017/04/11(1:结构体链表基础和相关经典操作)
  2. Greys Java在线问题诊断工具
  3. JVM内幕:Java虚拟机详解
  4. matlab中有哪些有趣的命令?好玩的matlab彩蛋
  5. Python dict dictionaries Python 数据结构——字典
  6. 局部特征(3)——SURF特征总结
  7. 数字图像处理:第十三章 图象复原
  8. Python基础教程(十二):GUI编程、版本区别、IDE
  9. 支持向量机SVM(四)
  10. 请求getServiceTime出错