获取json数据

来源:互联网 发布:软件试用期过了 编辑:程序博客网 时间:2024/06/04 19:00
package com.zking.administrator.g160628_android30_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;
    }
}


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;
    }
}



原创粉丝点击