Android Studio配置与使用GSON框架解析json数据

时间:2021-06-08 10:26:39

Android Studio配置与使用Gson框架解析json数据

[toc]
这里使用一个联动的spinner来演示何如使用Gson解析json数据

一、配置gson环境

Android Studio配置与使用GSON框架解析json数据

单击Open Module Settings

Android Studio配置与使用GSON框架解析json数据

单击Dependencies,点击右边的绿色加号

Android Studio配置与使用GSON框架解析json数据

选择选项的第一个Library Dependency

Android Studio配置与使用GSON框架解析json数据

输入GSON关键字,点击搜索

Android Studio配置与使用GSON框架解析json数据

这里选择一个合适的版本,然后点击OK

Android Studio配置与使用GSON框架解析json数据

这时候gson框架就加载好了

二、安装GSON插件

Android Studio配置与使用GSON框架解析json数据

Android Studio配置与使用GSON框架解析json数据

Android Studio配置与使用GSON框架解析json数据

注意插件安装完毕后请重启Android Studio

三、使用Gson插件生成Java Bean类

在src/main目录下(与java和res同级的目录下创建一个assets文件夹)
在此文件夹下复制一个地区编号的json文件
百度云下载地址:http://pan.baidu.com/s/1hrRAeCO

再创建一个bean包
包下创建一个名为AddrBean的类

在此类文件的任何位置右键

Android Studio配置与使用GSON框架解析json数据

Android Studio配置与使用GSON框架解析json数据

在弹出的对话框中将刚才下载文件的内容复制到这里

Android Studio配置与使用GSON框架解析json数据

Android Studio配置与使用GSON框架解析json数据

注意,这里自动生成的是中文的属性名,为了方便使用,我们将属性重命名

Android Studio配置与使用GSON框架解析json数据

然后单击下一步,一个Bean类就创建出来了

package com.doservlet.bean;

import com.google.gson.annotations.SerializedName;

import java.util.List;

/**
* Created by 陈小默
* http://my.oschina.net/oschenxiaomo/blog
* http://blog.csdn.net/qq_32583189
*/

public class AddrBean {

@SerializedName("城市代码")
private List<ProvinceBean> provinceList;

public List<ProvinceBean> getProvinceList() {
return provinceList;
}

public void setProvinceList(List<ProvinceBean> provinceList) {
this.provinceList = provinceList;
}

public static class ProvinceBean {
@SerializedName("省")
private String province;
/**
* 市名 : 北京
* 编码 : 101010100
*/


@SerializedName("市")
private List<CityBean> cityList;

public String getProvince() {
return province;
}

public void setProvince(String province) {
this.province = province;
}

public List<CityBean> getCityList() {
return cityList;
}

public void setCityList(List<CityBean> cityList) {
this.cityList = cityList;
}

public static class CityBean {
@SerializedName("市名")
private String cityName;
@SerializedName("编码")
private String cityCode;

public String getCityName() {
return cityName;
}

public void setCityName(String cityName) {
this.cityName = cityName;
}

public String getCityCode() {
return cityCode;
}

public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
}
}
}

四、创建一个布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.doservlet.activity.WeatherActivity">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">


<Spinner
android:id="@+id/province_list"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1">
</Spinner>

<Spinner
android:id="@+id/city_list"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1">
</Spinner>
</LinearLayout>
</LinearLayout>

五、为spinner创建适配器

显示省份的适配器

package com.doservlet.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.doservlet.bean.AddrBean;

import java.util.ArrayList;
import java.util.List;

/**
* Created by 陈小默
*/

public class ProvinceAdapter extends BaseAdapter {

private Context context;

public ProvinceAdapter(Context context) {
this.context = context;
}

private List<AddrBean.ProvinceBean> provinceBeanList = new ArrayList<>();

public void setProvinceBeanList(List<AddrBean.ProvinceBean> provinceBeanList) {
this.provinceBeanList = provinceBeanList;
notifyDataSetChanged();
}

@Override
public int getCount() {
return provinceBeanList.size();
}

@Override
public Object getItem(int position) {
return provinceBeanList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(context);
tv.setText(provinceBeanList.get(position).getProvince());
return tv;
}
}

市的适配器

package com.doservlet.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.doservlet.bean.AddrBean;

import java.util.ArrayList;
import java.util.List;

/**
* Created by 陈小默 on 2016/5/20.
*/

public class CityAdapter extends BaseAdapter {
private Context context;
private List<AddrBean.ProvinceBean.CityBean> cityBeanList = new ArrayList<>();

public void setCityBeanList(List<AddrBean.ProvinceBean.CityBean> cityBeanList) {
this.cityBeanList = cityBeanList;
notifyDataSetChanged();
}

public CityAdapter(Context context) {
this.context = context;
}

@Override
public int getCount() {
return cityBeanList.size();
}

@Override
public Object getItem(int position) {
return cityBeanList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(context);
tv.setText(cityBeanList.get(position).getCityName());
return tv;
}
}

六、在Activity中解析数据

package com.doservlet.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.doservlet.adapter.CityAdapter;
import com.doservlet.adapter.ProvinceAdapter;
import com.doservlet.bean.AddrBean;
import com.doservlet.bean.Weather;
import com.doservlet.xutilsdemo.R;
import com.google.gson.GsonBuilder;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.InputStream;
import java.io.InputStreamReader;

public class WeatherActivity extends AppCompatActivity {
private Spinner spProvince, spCity;
private AddrBean addrBean;
private ProvinceAdapter adpProvince;
private CityAdapter adpCity;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
initView();
loadData();
register();
}

private void register() {
spProvince.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
adpCity.setCityBeanList(addrBean.getProvinceList().get(position).getCityList());
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
}

private void loadData() {
try {
InputStream inputStream = getApplicationContext().getAssets().open("addr_china.json");

addrBean = new GsonBuilder().create().fromJson(new InputStreamReader(inputStream), AddrBean.class);

spProvince.setAdapter(adpProvince);
adpProvince.setProvinceBeanList(addrBean.getProvinceList());

spCity.setAdapter(adpCity);
adpCity.setCityBeanList(addrBean.getProvinceList().get(0).getCityList());
} catch (Throwable e) {
e.printStackTrace();
}
}

private void initView() {
spCity = (Spinner) findViewById(R.id.city_list);
spProvince = (Spinner) findViewById(R.id.province_list);

adpProvince = new ProvinceAdapter(this);
adpCity = new CityAdapter(this);
}
}

好了现在运行就能看到联动的结果了