项目短视频
操作示范如下(时长27:36)

【例7-4】解析网络JSON数据
不用另外设计,使用【例7-3】的布局文件activity_volley.xml。
准备自己搭建一个服务器,放一个JSON文件jsonData.json,或者去外网访问JSON数据,例如
http://www.weather.com.cn/data/cityinfo/101010100.html
jsonData.json文件
{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"18℃","temp2":"31℃","weather":"多云转阴", "img1":"d1.gif", "img2":"n1.gif", "ptime":"08:00"}}
该文件可以尝试以下链接:
http://pan-yz.chaoxing.com/external/m/file/634801254428688384
Javabean文件 Weatherinfo.java源代码如下:
package com.example.chap07;
//练习 javabean, 项目也可以不用 javabean
public class Weatherinfo {
/** JSON数据样本
* city : 北京
* cityid : 101010100
* temp1 : 15℃
* temp2 : 5℃
* weather : 多云
* img1 : d1.gif
* img2 : n1.gif
* ptime : 08:00
*/
private String city;
private String cityid;
private String temp1;
private String temp2;
private String weather;
private String img1;
private String img2;
private String ptime;
public Weatherinfo(){
}
public Weatherinfo(String city, String cityid, String temp1,
String temp2, String weather,String img1, String img2,String ptime)
{
this.city = city;
this.cityid = cityid;
this.temp1 = temp1;
this.temp2 = temp2;
this.weather = weather;
this.img1 = img1;
this.img2 = img2;
this.ptime = ptime;
}
@Override
public String toString() {
return "WeatherInfo [city=" + city + ", cityid=" + cityid + ", temp1="
+ temp1 + ", temp2=" + temp2 + ", weather=" + weather
+ ", ptime=" + ptime + "]";
}
}
主控制程序WeatherActivity.java的源代码如下:
package com.example.chap07;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
/*JSON数据样本
{ "weatherinfo": {
"city": "北京",
"cityid": "101010100",
"temp1": "18℃",
"temp2": "31℃",
"weather": "多云转阴",
"img1": "d1.gif",
"img2": "n1.gif",
"ptime": "08:00"
}
}*/
//【例7-4】应用Volley框架解析JSON数据。
public class WeatherActivity extends AppCompatActivity
implements View.OnClickListener {
Button Btn;
TextView txt;
String url = "http://www.weather.com.cn/data/cityinfo/101010100.html";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_volley);
Btn = (Button) findViewById(R.id.btn);
txt = (TextView) findViewById(R.id.txt);
Btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
JsonObjectRequest stringRequest= new JsonObjectRequest(
//第 1个参数,目标服务器的URL地址
url,
//第2个参数,JSONObject
null,
//第3个参数,服务器响应成功的回调
new Response.Listener<JSONObject>(){
@Override
public void onResponse(JSONObject response) {
try {
JSONObject mjson=response.getJSONObject("weatherinfo"); //Jason数据的键值对的键key
String city=mjson.getString("city");
String cityid=mjson.getString("cityid");
String temp1=mjson.getString("temp1");
String temp2=mjson.getString("temp2");
String weather=mjson.getString("weather");
String img1=mjson.getString("img1");
String img2=mjson.getString("img2");
String ptime=mjson.getString("ptime");
String s="[city=" + city + ", cityid=" + cityid + ", temp1="
+ temp1 + ", temp2=" + temp2 + ", weather=" + weather
+ ", ptime=" + ptime + "]";
txt.setText(s);
//加入一个 Javabean 的练习
Weatherinfo bean = new Weatherinfo(city, cityid, temp1,
temp2, weather,img1, img2, ptime);
Log.d("TAG", bean.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
},
//第4个参数,服务器响应失败的回调
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
txt.setText("网页访问失败");
}
}){
@Override
protected Response<JSONObject> parseNetworkResponse(
NetworkResponse response) {
try {
JSONObject jsonObject = new JSONObject(
new String(response.data, "UTF-8"));//将默认的ISO-8859-1转换为utf-8
return Response.success(jsonObject,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (Exception je) {
return Response.error(new ParseError(je)) ;
}
}
};
RequestQueue mQueue = Volley.newRequestQueue(this);
mQueue.add(stringRequest);
}
}
打开项目配置文件AndroidManifest.xml,检查网络权限,确保工程要启动的Activity类名为

