1.为了快速演示一下解析json文件,所以就直接在Tomcat的root文件加下新建一个students.json文件
其演示代码如下:
{"count":3,
"students":
[
{"sid":1,"sname":"么么","ssex":"男"},
{"sid":2,"sname":"妞妞","ssex":"女"},
{"sid":3,"sname":"皮皮","ssex":"男"}
]
}
2.在布局文件中写一个按钮代码就省略了
3.java代码如下:
分析一下演示的JSON类,可以知道里面一个有2个对象
新建一个BigObject类,代码如下
package com.example.cookie.android0726parsexml;
import java.util.List;
/**
* Created by Administrator on 2017/7/26 0026.
*/
public class BigObject {
private int count;
private List<Student> students;
public BigObject(int count, List<Student> students) {
this.count = count;
this.students = students;
}
public BigObject() {
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
新建一个Student类,其代码如下:
package com.example.cookie.android0726parsexml;
/**
* Created by Administrator on 2017/7/26 0026.
*/
public class Student {
private int sid;
private String sname;
private String ssex;
public Student() {
}
public Student(int sid, String sname, String ssex) {
this.sid = sid;
this.sname = sname;
this.ssex = ssex;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
}
package com.example.cookie.android0726parsexml;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import com.alibaba.fastjson.JSON;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pd = new ProgressDialog(this);
pd.setMessage("正在拼命加载中。。。。。。。。。。。");
}
public void getJsonByNet(View view){
new MyTaskNet().execute();
}
class MyTaskNet extends AsyncTask{
@Override
protected void onPreExecute() {
super.onPreExecute();
pd.show();
}
@Override
protected Object doInBackground(Object[] params) {
//获取网络JSON数据
String path="http://193.168.4.232:8080/students.json";
try {
URL url=new URL(path);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
if (connection.getResponseCode()==200){
InputStream is=connection.getInputStream();
int len=0;
byte[] buf=new byte[1024];
StringBuffer stringBuffer=new StringBuffer();
while((len=is.read(buf))!=-1){
String s=new String(buf,0,len);
stringBuffer.append(s);
}
Log.i("test",stringBuffer.toString());
//解析
BigObject bigObject=JSON.parseObject(stringBuffer.toString(),BigObject.class);
int count=bigObject.getCount();
Log.i("test","count:"+count);
List<Student> students=bigObject.getStudents();
for (Student student : students) {
Log.i("test",student.getSname()+" "+student.getSsex()+" "+student.getSname());
}
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
pd.cancel();
}
}
}