使用Volley解析json数据
本篇文章只是简单介绍Volley解析Json数据,其中以封装好的Gson对象作为一个例子,以便大家方便开发。
在本例子中需要用到的资源你可以点击以下链接:
http://download.csdn.net/download/json998/8843493
测试需要用到的json数据地址:
https://code.csdn.net/snippets/719107/master/snippet_file_0.txt/raw
下面就不多说了,我直接贴出代码供大家参阅,请指教:
解析json需要的Bean如下:
这是最外层的Bean:
package com.bean;
import java.util.List;
public class Cai {
private String status;
private List categories;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List getCategories() {
return categories;
}
public void setCategories(List categories) {
this.categories = categories;
}
}
以下是Categories实体类:
package com.bean;
import java.util.List;
public class Categories {
private String category_name;
private List subcategories;
public String getCategory_name() {
return category_name;
}
public void setCategory_name(String category_name) {
this.category_name = category_name;
}
public List getSubcategories() {
return subcategories;
}
public void setSubcategories(List subcategories) {
this.subcategories = subcategories;
}
}
以下是Subcategories实体类
package com.bean;
import java.util.List;
public class Subcategories {
private String category_name;
private List subcategories;
public String getCategory_name() {
return category_name;
}
public void setCategory_name(String category_name) {
this.category_name = category_name;
}
public List getSubcategories() {
return subcategories;
}
public void setSubcategories(List subcategories) {
this.subcategories = subcategories;
}
}
这样根据json数据格式,对应的实体就写完了,以下是具体的解析过程如下:
我们编写一个类MyApplicationContext extends Application 代码如下:
package com;
import com.android.volley.extra.RequestManager;
import android.app.Application;
public class MyApplicationContext extends Application {
@Override
public void onCreate() {
super.onCreate();
RequestManager.DEFAULT_CONTENT_CHARSET = “UTF-8”;
//框架的初始化
RequestManager.init(getBaseContext());
}
}
我们接着编写一个MainActivity extends Activity 代码如下:
package com.wcn.testvolley;
import java.util.List;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.extra.GsonRequest;
import com.android.volley.extra.RequestManager;
import com.bean.Cai;
import com.bean.Categories;
import com.bean.Subcategories;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "https://code.csdn.net/snippets/719107/master/snippet_file_0.txt/raw";
GsonRequest gsonRequest = new GsonRequest(url, Cai.class, new Response.Listener() {
@Override
public void onResponse(Cai response) {
if(response !=null){
List categories = response.getCategories();
for(Categories cate : categories){
String Category_name = cate.getCategory_name();
Log.e("Category_name", Category_name);
List subcategories = cate.getSubcategories();
for(Subcategories sub : subcategories){
String category_name = sub.getCategory_name();
Log.e("category_name", category_name);
List subcategories2 = sub.getSubcategories();
for(String caiName : subcategories2){
Log.e("caiName", caiName);
}
}
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//添加请求
RequestManager.addRequest(gsonRequest, MainActivity.this);
}
@Override
protected void onDestroy() {
super.onDestroy();
RequestManager.cancelAll(MainActivity.this);
}
}
最后就是AndroidManifest.xml文件 如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wcn.testvolley"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name="com.MyApplicationContext"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
application>
manifest>
由于例子中不涉及界面,我们直接将数据解析出来并Log显示,所以默认界面就可以了。
以上就是全部的代码,相信你们一看就明白了,如有不清楚的地方,欢迎吐槽,谢谢各位看客。