簡單使用了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.retrofitdemoimport android
.os.Bundle
import android
.support.v7
.app.AppCompatActivity
import android
.widget.ListViewimport java
.util.ArrayList
import java
.util.Listimport butterknife
.BindView
import butterknife
.ButterKnife
import retrofit2
.Call
import retrofit2
.Callback
import retrofit2
.Response
import retrofit2
.Retrofit
import retrofit2
.converter.gson.GsonConverterFactorypublic class MainActivity extends AppCompatActivity implements Callback<Tngou> {@BindView(R
.id.news_listView)ListView newsListViewprivate NewsAdapter adapterprivate 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数据简单实例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。