获取json数据
来源:互联网 发布:软件试用期过了 编辑:程序博客网 时间:2024/06/04 19:00
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;
}
}
package com.zking.administrator.g160628_android30_parsexml;
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 getXMLByNet(View view){
new MyTask().execute();
}
class MyTask extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
//获取网络XML数据
String path="http://192.168.43.77:7788/students.xml";
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());
//解析XML (PULL)
XmlPullParser pullParser=Xml.newPullParser();
pullParser.setInput(is,"UTF-8");
//解析的标签类型
int type=pullParser.getEventType();
while(type!=XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_TAG:
//获取开始标签的名字
String startTagName=pullParser.getName();
if("student".equals(startTagName)){
//获取属性id的值
String sid=pullParser.getAttributeValue(0);
Log.i("test",sid);
}else if("sname".equals(startTagName)){
String sname=pullParser.nextText();
Log.i("test"," "+sname);
}else if("sage".equals(startTagName)){
String sage=pullParser.nextText();
Log.i("test"," "+sage);
}
break;
case XmlPullParser.END_TAG:
break;
}
//细节:
type=pullParser.next();
}
is.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
}
}
public void getJsonByNet(View view){
new MyJsonTask().execute();
}
class MyJsonTask extends AsyncTask{
@Override
protected void onPreExecute() {
super.onPreExecute();
pd.show();
}
@Override
protected Object doInBackground(Object[] params) {
//获取网络Json数据
String path="http://192.168.43.77:7788/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());
//Gson FastJson
BigObject bigObject=JSON.parseObject(stringBuffer.toString(),BigObject.class);
int count=bigObject.getCount();
Log.i("test",count+" ");
List<Student> students=bigObject.getStudents();
for (Student student : students) {
Log.i("test",student.getSname()+" "+student.getSsex());
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
pd.cancel();
super.onPostExecute(o);
}
}
}
package com.zking.administrator.g160628_android30_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;
}
}
- PHP获取JSON数据
- json封装,获取数据
- 获取解析JSON数据
- json数据获取
- json数据获取
- 获取服务器端JSON数据
- ajax获取json数据
- jQuery获取json数据
- Jquery获取json数据
- Ajax获取Json数据
- 获取JSON数据客户端
- 获取json数据
- jsonp获取json数据
- 获取json数据
- AJAX获取JSON数据
- listview获取json数据
- AngularJS 获取JSON数据
- jQuery获取JSON数据
- js屏蔽手机返回键
- 第二章、flume的安装
- 从TensorFlow到Theano:横向对比七大深度学习框架
- hdu 2019 数列有序!
- 10 在ZStack里的按键驱动
- 获取json数据
- 图片轮播jquery , 支持html5 , 滑动手势, 屏幕大小自适配 , 横排 竖排图片自适应, 竖排图片居中显示
- c++四种类型转换方式
- 线程--(1)ThreadLocal简单使用
- <需要补充学习的资料>: AVL tree , Tire tree(前缀树(prefix tree),后缀树(suffix tree)),radix tree, aho-corasick
- C#之枚举类型总结
- POJ 3461 Oulipo(KMP统计子串出现次数)
- 【数据】Marvell 88W8686 WiFi模块的固件数据
- [霍尔定理]「2017 山东一轮集训 Day2」LOJ 6062——PAIR