我们先在apache-tomcat-9.0.0.M17\webapps\ROOT 中创建一个后缀为json 里面放些参数 等下用来解析它 如我们这用了一个students.json:
在导入一个包 fastjson-1.2.3.jar
因为我们要解析的对象是json 所以依据内容我们要创建两实体类。
Student实体类:
package com.example.g160628_android31_parsexml; /** * 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; } }
然后是BigObject实体类
package com.example.g160628_android31_parsexml; import java.util.List; /** * Created by Administrator on 2017/7/26 0026. */ public class BigObject { private int count; private List<Student> students; public BigObject() { } public BigObject(int count, List<Student> students) { this.count = count; this.students = students; } 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; } }
最后是 MainActivity.java
package com.example.g160628_android31_parsexml;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.alibaba.fastjson.JSON;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.AsynchronousCloseException;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void getJsonByNet(View view){
new MyJsonTask().execute();
}
class MyJsonTask extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
//获取网络json数据
String path="http://localhost:7788/students.json";
try {
URL url=new URL(path);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
//设置请求方式
connection.setRequestMethod("GET");
//设置请求超时的时间
connection.setConnectTimeout(5000);
//获取结果码
int code=connection.getResponseCode();
if(code==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());
//Gson fastJson
//解析Json
BigObject bigObject=JSON.parseObject(stringBuffer.toString(),BigObject.class);
int count=bigObject.getCount();
List<Student> students=bigObject.getStudents();
for (Student student : students) {
Log.i("test",student.getSname()+""+student.getSsex());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
如果在手机上运行还要加个权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>