Android网络数据Json格式解析

     上次我们讲到XML解析网络数据,这次我们来研究研究用Json格式解析数据


    照旧,我们先在web端的FQAcgtion.class模拟一组Json格式的数据:

 

[java] view plain copy
print ?
  1. public String getJson() throws Exception {  
  2.         // 获取数据  
  3.         // 调用数据库查询数据,返回对象集合(….)  
  4.         List fqs = new ArrayList();  
  5.         for (int i = 1; i <= 100; i++) {  
  6.             Calendar calendar = Calendar.getInstance();  
  7.             int year = calendar.get(Calendar.YEAR);  
  8.             int month = calendar.get(Calendar.MONTH);  
  9.             int day = calendar.get(Calendar.DAY_OF_MONTH);  
  10.             fqs.add(new FQ(“原生态” + i, “很纯”, year + “-“ + month + “-“ + day));  
  11.         }  
  12.           
  13.         //将对象集合转换为json,例如:{“class”:”150831”,”lists”:30,”fqs”“[{},{},{}]}  
  14.         JSONObject jo=new JSONObject();  
  15.         jo.put(”clazz”“150831”);  
  16.         jo.put(”lists”, fqs.size());  
  17.           
  18.         JSONArray ja=new JSONArray();  
  19.           
  20.         for (FQ fq : fqs) {  
  21.             JSONObject jos=new JSONObject();  
  22.             jos.put(”name”, fq.getName());  
  23.             jos.put(”content”, fq.getContent());  
  24.             jos.put(”time”, fq.getTime());  
  25.             ja.add(jos);  
  26.         }  
  27.           
  28.         jo.put(”fqs”, ja);  
  29.           
  30.   
  31.         // 将对象集合存放到请求域中  
  32. //      ServletActionContext.getRequest().setAttribute(“fqs”, jo.toString());  
  33.   
  34.         return “dataResultJson”;  
  35.     }  
  36.       
public String getJson() throws Exception {
        // 获取数据
        // 调用数据库查询数据,返回对象集合(....)
        List fqs = new ArrayList();
        for (int i = 1; i <= 100; i++) {
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            fqs.add(new FQ("原生态" + i, "很纯", year + "-" + month + "-" + day));
        }

        //将对象集合转换为json,例如:{"class":"150831","lists":30,"fqs""[{},{},{}]}
        JSONObject jo=new JSONObject();
        jo.put("clazz", "150831");
        jo.put("lists", fqs.size());

        JSONArray ja=new JSONArray();

        for (FQ fq : fqs) {
            JSONObject jos=new JSONObject();
            jos.put("name", fq.getName());
            jos.put("content", fq.getContent());
            jos.put("time", fq.getTime());
            ja.add(jos);
        }

        jo.put("fqs", ja);


        // 将对象集合存放到请求域中
//      ServletActionContext.getRequest().setAttribute("fqs", jo.toString());

        return "dataResultJson";
    }
    

      dataResultJson.jsp:

[java] view plain copy
print ?
  1. <%@ page language=“java” contentType=“text/plain; charset=utf-8” pageEncoding=“utf-8”%>{fqs}  
<%@ page language="java" contentType="text/plain; charset=utf-8" pageEncoding="utf-8"%>{fqs}
   struts.xml配置:

[java] view plain copy
print ?
  1. “fqAction*” class=“com.zking.action.FQAcgtion” method=“{1}”>  
  2.             ”dataResult”>/dataResult.jsp    
  3.             ”dataResult”>/dataResultJson.jsp            
  4.           

            /dataResult.jsp  
            /dataResultJson.jsp          
        



    截图如下:

Android网络数据Json格式解析_第1张图片

Android网络数据Json格式解析_第2张图片

Android网络数据Json格式解析_第3张图片



        数据模拟好了,那我们现在来Android端拿取数据

       首先,先来个XML页面:依然是一个按钮和一个listview展示数据

      activity_get_json.xml

[java] view plain copy
print ?
  1.         android:layout_width=”match_parent”  
  2.         android:layout_height=”wrap_content”  
  3.         android:text=”获取Json”  
  4.         android:onClick=”getJson”  
  5.         />  
  6.   
  7.     
  8.         android:layout_width=”match_parent”  
  9.         android:layout_height=”wrap_content”  
  10.         android:id=”@+id/lv_json_list”  
  11.         >  
  12.       

     XML解析有三种方式,同样Json解析也有三种方式:


    1、原生态

     activity_get_json.activity

[java] view plain copy
print ?
  1.   
    
[java] view plain copy
print ?
  1. “background-color: rgb(240, 240, 240);”>public class GetJsonActivity extends AppCompatActivity {

        
    private ListView lv_json_list;  
  2.     private List lists=new ArrayList<>();  
  3.     private MyAdapter myAdapter;  
  4.     private ProgressDialog progressDialog;  
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_get_json);  
  9.   
  10.         lv_json_list = (ListView) findViewById(R.id.lv_json_list);  
  11.         myAdapter = new MyAdapter();  
  12.         lv_json_list.setAdapter(myAdapter);  
  13.   
  14.         progressDialog = new ProgressDialog(this);  
  15.         progressDialog.setMessage(”正在拼命loading中…”);  
  16.     }  
  17.     class MyAdapter extends BaseAdapter {  
  18.   
  19.         @Override  
  20.         public int getCount() {  
  21.             return lists.size();  
  22.         }  
  23.   
  24.         @Override  
  25.         public Object getItem(int i) {  
  26.             return lists.get(i);  
  27.         }  
  28.   
  29.         @Override  
  30.         public long getItemId(int i) {  
  31.             return i;  
  32.         }  
  33.   
  34.         @Override  
  35.         public View getView(int i, View view, ViewGroup viewGroup) {  
  36.   
  37.             if(view==null){  
  38.                 view= LayoutInflater.from(GetJsonActivity.this).inflate(R.layout.item_listview,null);  
  39.   
  40.                 ItemTag itemTag=new ItemTag();  
  41.   
  42.                 itemTag.tv_content= (TextView) view.findViewById(R.id.tv_item_listview_content);  
  43.                 itemTag.tv_name= (TextView) view.findViewById(R.id.tv_item_listview_name);  
  44.                 itemTag.tv_time= (TextView) view.findViewById(R.id.tv_item_listview_time);  
  45.   
  46.                 view.setTag(itemTag);  
  47.             }  
  48.   
  49.             ItemTag itemTag= (ItemTag) view.getTag();  
  50.             itemTag.tv_name.setText(lists.get(i).getName());  
  51.             itemTag.tv_content.setText(lists.get(i).getContent());  
  52.             itemTag.tv_time.setText(lists.get(i).getTime());  
  53.   
  54.             return view;  
  55.         }  
  56.     }  
  57.   
  58.     public void getJson(View view){  
  59.             new MyTask().execute();  
  60.     }  
  61.   
  62.     class MyTask extends AsyncTask{  
  63.   
  64.         @Override  
  65.         protected void onPreExecute() {  
  66.             super.onPreExecute();  
  67.             progressDialog.show();  
  68.         }  
  69.   
  70.         @Override  
  71.         protected Object doInBackground(Object[] objects) {  
  72.             String path=getString(R.string.server_name)+”fqActiongetJson.action”;  
  73.             try {  
  74.                 URL url=new URL(path);  
  75.                 HttpURLConnection connection= (HttpURLConnection) url.openConnection();  
  76.                 connection.setRequestMethod(”GET”);  
  77.                 connection.setConnectTimeout(5000);  
  78.   
  79.   
  80.                 if(connection.getResponseCode()==200){  
  81.                     InputStream is=connection.getInputStream();  
  82.                     //读  
  83.                     BufferedReader br=new BufferedReader(new InputStreamReader(is));  
  84.                     StringBuffer stringBuffer=new StringBuffer();  
  85.                     String str=null;  
  86.                     while((str=br.readLine())!=null){  
  87.                         stringBuffer.append(str);  
  88.                     }  
  89. //                    Log.i(“test”,stringBuffer.toString());  
  90.   
  91.                     //解析JSOn  
  92.                     //01.原生态  
  93. //                    try {  
  94. //                        JSONObject jsonObject=new JSONObject(stringBuffer.toString());  
  95. //                        String clazz=jsonObject.getString(“class”);  
  96. //                        Log.i(“test”,”class:”+clazz);  
  97. //                        int num=jsonObject.getInt(“lists”);  
  98. //                        Log.i(“test”,”lists:”+num);  
  99. //  
  100. //                        JSONArray jsonArray=jsonObject.getJSONArray(“fqs”);  
  101. //                        for (int i = 0; i   
  102. //                            JSONObject object=jsonArray.getJSONObject(i);  
  103. //                            String name=object.getString(“name”);  
  104. //                            String content=object.getString(“content”);  
  105. //                            String time=object.getString(“time”);  
  106. //                            FQ fq=new FQ(name,content,time);  
  107. //                            lists.add(fq);  
  108. //                        }  
  109. //                    } catch (JSONException e) {  
  110. //                        e.printStackTrace();  
  111. //                    }  
  112.   
  113.   
  114.                       
  115.   
  116.             return null;  
  117.         }  
  118.   
  119.         @Override  
  120.         protected void onPostExecute(Object o) {  
  121.             super.onPostExecute(o);  
  122.   
  123.             myAdapter.notifyDataSetChanged();  
  124.   
  125.             progressDialog.cancel();  
  126.         }  
  127.     }  
  128.   
  129.   
  130. }  

public class GetJsonActivity extends AppCompatActivity {

private ListView lv_json_list; private List lists=new ArrayList<>(); private MyAdapter myAdapter; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_get_json); lv_json_list = (ListView) findViewById(R.id.lv_json_list); myAdapter = new MyAdapter(); lv_json_list.setAdapter(myAdapter); progressDialog = new ProgressDialog(this); progressDialog.setMessage("正在拼命loading中..."); } class MyAdapter extends BaseAdapter { @Override public int getCount() { return lists.size(); } @Override public Object getItem(int i) { return lists.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if(view==null){ view= LayoutInflater.from(GetJsonActivity.this).inflate(R.layout.item_listview,null); ItemTag itemTag=new ItemTag(); itemTag.tv_content= (TextView) view.findViewById(R.id.tv_item_listview_content); itemTag.tv_name= (TextView) view.findViewById(R.id.tv_item_listview_name); itemTag.tv_time= (TextView) view.findViewById(R.id.tv_item_listview_time); view.setTag(itemTag); } ItemTag itemTag= (ItemTag) view.getTag(); itemTag.tv_name.setText(lists.get(i).getName()); itemTag.tv_content.setText(lists.get(i).getContent()); itemTag.tv_time.setText(lists.get(i).getTime()); return view; } } public void getJson(View view){ new MyTask().execute(); } class MyTask extends AsyncTask{ @Override protected void onPreExecute() { super.onPreExecute(); progressDialog.show(); } @Override protected Object doInBackground(Object[] objects) { String path=getString(R.string.server_name)+"fqActiongetJson.action"; 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(); //读 BufferedReader br=new BufferedReader(new InputStreamReader(is)); StringBuffer stringBuffer=new StringBuffer(); String str=null; while((str=br.readLine())!=null){ stringBuffer.append(str); } // Log.i("test",stringBuffer.toString()); //解析JSOn //01.原生态 // try { // JSONObject jsonObject=new JSONObject(stringBuffer.toString()); // String clazz=jsonObject.getString("class"); // Log.i("test","class:"+clazz); // int num=jsonObject.getInt("lists"); // Log.i("test","lists:"+num); // // JSONArray jsonArray=jsonObject.getJSONArray("fqs"); // for (int i = 0; i


      2、GJon解析

[java] view plain copy
print ?
  1. public class GetJsonActivity extends AppCompatActivity {  
  2.   
  3.     private ListView lv_json_list;  
  4.     private List lists=new ArrayList<>();  
  5.     private MyAdapter myAdapter;  
  6.     private ProgressDialog progressDialog;  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_get_json);  
  11.   
  12.         lv_json_list = (ListView) findViewById(R.id.lv_json_list);  
  13.         myAdapter = new MyAdapter();  
  14.         lv_json_list.setAdapter(myAdapter);  
  15.   
  16.         progressDialog = new ProgressDialog(this);  
  17.         progressDialog.setMessage(”正在拼命loading中…”);  
  18.     }  
  19.     class MyAdapter extends BaseAdapter {  
  20.   
  21.         @Override  
  22.         public int getCount() {  
  23.             return lists.size();  
  24.         }  
  25.   
  26.         @Override  
  27.         public Object getItem(int i) {  
  28.             return lists.get(i);  
  29.         }  
  30.   
  31.         @Override  
  32.         public long getItemId(int i) {  
  33.             return i;  
  34.         }  
  35.   
  36.         @Override  
  37.         public View getView(int i, View view, ViewGroup viewGroup) {  
  38.   
  39.             if(view==null){  
  40.                 view= LayoutInflater.from(GetJsonActivity.this).inflate(R.layout.item_listview,null);  
  41.   
  42.                 ItemTag itemTag=new ItemTag();  
  43.   
  44.                 itemTag.tv_content= (TextView) view.findViewById(R.id.tv_item_listview_content);  
  45.                 itemTag.tv_name= (TextView) view.findViewById(R.id.tv_item_listview_name);  
  46.                 itemTag.tv_time= (TextView) view.findViewById(R.id.tv_item_listview_time);  
  47.   
  48.                 view.setTag(itemTag);  
  49.             }  
  50.   
  51.             ItemTag itemTag= (ItemTag) view.getTag();  
  52.             itemTag.tv_name.setText(lists.get(i).getName());  
  53.             itemTag.tv_content.setText(lists.get(i).getContent());  
  54.             itemTag.tv_time.setText(lists.get(i).getTime());  
  55.   
  56.             return view;  
  57.         }  
  58.     }  
  59.   
  60.     public void getJson(View view){  
  61.             new MyTask().execute();  
  62.     }  
  63.   
  64.     class MyTask extends AsyncTask{  
  65.   
  66.         @Override  
  67.         protected void onPreExecute() {  
  68.             super.onPreExecute();  
  69.             progressDialog.show();  
  70.         }  
  71.   
  72.         @Override  
  73.         protected Object doInBackground(Object[] objects) {  
  74.             String path=getString(R.string.server_name)+”fqActiongetJson.action”;  
  75.             try {  
  76.                 URL url=new URL(path);  
  77.                 HttpURLConnection connection= (HttpURLConnection) url.openConnection();  
  78.                 connection.setRequestMethod(”GET”);  
  79.                 connection.setConnectTimeout(5000);  
  80.   
  81.   
  82.                 if(connection.getResponseCode()==200){  
  83.                     InputStream is=connection.getInputStream();  
  84.                     //读  
  85.                     BufferedReader br=new BufferedReader(new InputStreamReader(is));  
  86.                     StringBuffer stringBuffer=new StringBuffer();  
  87.                     String str=null;  
  88.                     while((str=br.readLine())!=null){  
  89.                         stringBuffer.append(str);  
  90.                     }  
  91. //                    Log.i(“test”,stringBuffer.toString());  
  92.   
  93.                     //解析JSOn  
  94.                     
  95.   
  96.                     //02.使用Gson解析json  
  97. //                    Gson gson=new Gson();  
  98. //                    BigFQ bigFQ=gson.fromJson(stringBuffer.toString(),BigFQ.class);  
  99. //  
  100. //                    String clazz=bigFQ.getClazz();  
  101. //                    int num=bigFQ.getLists();  
  102. //  
  103. //                    Log.i(“test”,”clazz ”+clazz+” num:”+num);  
  104. //                    lists.addAll(bigFQ.getFqs());  
  105.   
  106.                   
  107.                 }  
  108.             } catch (MalformedURLException e) {  
  109.                 e.printStackTrace();  
  110.             } catch (IOException e) {  
  111.                 e.printStackTrace();  
  112.             }  
  113.   
  114.   
  115.             return null;  
  116.         }  
  117.   
  118.         @Override  
  119.         protected void onPostExecute(Object o) {  
  120.             super.onPostExecute(o);  
  121.   
  122.             myAdapter.notifyDataSetChanged();  
  123.   
  124.             progressDialog.cancel();  
  125.         }  
  126.     }  
  127.   
  128.   
  129. }  
public class GetJsonActivity extends AppCompatActivity {

    private ListView lv_json_list;
    private List lists=new ArrayList<>();
    private MyAdapter myAdapter;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_json);

        lv_json_list = (ListView) findViewById(R.id.lv_json_list);
        myAdapter = new MyAdapter();
        lv_json_list.setAdapter(myAdapter);

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在拼命loading中...");
    }
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public Object getItem(int i) {
            return lists.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            if(view==null){
                view= LayoutInflater.from(GetJsonActivity.this).inflate(R.layout.item_listview,null);

                ItemTag itemTag=new ItemTag();

                itemTag.tv_content= (TextView) view.findViewById(R.id.tv_item_listview_content);
                itemTag.tv_name= (TextView) view.findViewById(R.id.tv_item_listview_name);
                itemTag.tv_time= (TextView) view.findViewById(R.id.tv_item_listview_time);

                view.setTag(itemTag);
            }

            ItemTag itemTag= (ItemTag) view.getTag();
            itemTag.tv_name.setText(lists.get(i).getName());
            itemTag.tv_content.setText(lists.get(i).getContent());
            itemTag.tv_time.setText(lists.get(i).getTime());

            return view;
        }
    }

    public void getJson(View view){
            new MyTask().execute();
    }

    class MyTask extends AsyncTask{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.show();
        }

        @Override
        protected Object doInBackground(Object[] objects) {
            String path=getString(R.string.server_name)+"fqActiongetJson.action";
            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();
                    //读
                    BufferedReader br=new BufferedReader(new InputStreamReader(is));
                    StringBuffer stringBuffer=new StringBuffer();
                    String str=null;
                    while((str=br.readLine())!=null){
                        stringBuffer.append(str);
                    }
//                    Log.i("test",stringBuffer.toString());

                    //解析JSOn


                    //02.使用Gson解析json
//                    Gson gson=new Gson();
//                    BigFQ bigFQ=gson.fromJson(stringBuffer.toString(),BigFQ.class);
//
//                    String clazz=bigFQ.getClazz();
//                    int num=bigFQ.getLists();
//
//                    Log.i("test","clazz "+clazz+" num:"+num);
//                    lists.addAll(bigFQ.getFqs());


                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);

            myAdapter.notifyDataSetChanged();

            progressDialog.cancel();
        }
    }


}


     3、FastJson解析

[java] view plain copy
print ?
  1. public class GetJsonActivity extends AppCompatActivity {  
  2.   
  3.     private ListView lv_json_list;  
  4.     private List lists=new ArrayList<>();  
  5.     private MyAdapter myAdapter;  
  6.     private ProgressDialog progressDialog;  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_get_json);  
  11.   
  12.         lv_json_list = (ListView) findViewById(R.id.lv_json_list);  
  13.         myAdapter = new MyAdapter();  
  14.         lv_json_list.setAdapter(myAdapter);  
  15.   
  16.         progressDialog = new ProgressDialog(this);  
  17.         progressDialog.setMessage(”正在拼命loading中…”);  
  18.     }  
  19.     class MyAdapter extends BaseAdapter {  
  20.   
  21.         @Override  
  22.         public int getCount() {  
  23.             return lists.size();  
  24.         }  
  25.   
  26.         @Override  
  27.         public Object getItem(int i) {  
  28.             return lists.get(i);  
  29.         }  
  30.   
  31.         @Override  
  32.         public long getItemId(int i) {  
  33.             return i;  
  34.         }  
  35.   
  36.         @Override  
  37.         public View getView(int i, View view, ViewGroup viewGroup) {  
  38.   
  39.             if(view==null){  
  40.                 view= LayoutInflater.from(GetJsonActivity.this).inflate(R.layout.item_listview,null);  
  41.   
  42.                 ItemTag itemTag=new ItemTag();  
  43.   
  44.                 itemTag.tv_content= (TextView) view.findViewById(R.id.tv_item_listview_content);  
  45.                 itemTag.tv_name= (TextView) view.findViewById(R.id.tv_item_listview_name);  
  46.                 itemTag.tv_time= (TextView) view.findViewById(R.id.tv_item_listview_time);  
  47.   
  48.                 view.setTag(itemTag);  
  49.             }  
  50.   
  51.             ItemTag itemTag= (ItemTag) view.getTag();  
  52.             itemTag.tv_name.setText(lists.get(i).getName());  
  53.             itemTag.tv_content.setText(lists.get(i).getContent());  
  54.             itemTag.tv_time.setText(lists.get(i).getTime());  
  55.   
  56.             return view;  
  57.         }  
  58.     }  
  59.   
  60.     public void getJson(View view){  
  61.             new MyTask().execute();  
  62.     }  
  63.   
  64.     class MyTask extends AsyncTask{  
  65.   
  66.         @Override  
  67.         protected void onPreExecute() {  
  68.             super.onPreExecute();  
  69.             progressDialog.show();  
  70.         }  
  71.   
  72.         @Override  
  73.         protected Object doInBackground(Object[] objects) {  
  74.             String path=getString(R.string.server_name)+”fqActiongetJson.action”;  
  75.             try {  
  76.                 URL url=new URL(path);  
  77.                 HttpURLConnection connection= (HttpURLConnection) url.openConnection();  
  78.                 connection.setRequestMethod(”GET”);  
  79.                 connection.setConnectTimeout(5000);  
  80.   
  81.   
  82.                 if(connection.getResponseCode()==200){  
  83.                     InputStream is=connection.getInputStream();  
  84.                     //读  
  85.                     BufferedReader br=new BufferedReader(new InputStreamReader(is));  
  86.                     StringBuffer stringBuffer=new StringBuffer();  
  87.                     String str=null;  
  88.                     while((str=br.readLine())!=null){  
  89.                         stringBuffer.append(str);  
  90.                     }  
  91. //                    Log.i(“test”,stringBuffer.toString());  
  92.   
  93.                     //解析JSOn  
  94.                      
  95.                     //03.使用FastJson  
  96.                     BigFQ bigFQ=JSON.parseObject(stringBuffer.toString(),BigFQ.class);  
  97.                     String clazz=bigFQ.getClazz();  
  98.                     int num=bigFQ.getLists();  
  99.   
  100.                     Log.i(”test”,“clazz ”+clazz+“ num:”+num);  
  101.                     lists.addAll(bigFQ.getFqs());  
  102.   
  103.                 }  
  104.             } catch (MalformedURLException e) {  
  105.                 e.printStackTrace();  
  106.             } catch (IOException e) {  
  107.                 e.printStackTrace();  
  108.             }  
  109.   
  110.   
  111.             return null;  
  112.         }  
  113.   
  114.         @Override  
  115.         protected void onPostExecute(Object o) {  
  116.             super.onPostExecute(o);  
  117.   
  118.             myAdapter.notifyDataSetChanged();  
  119.   
  120.             progressDialog.cancel();  
  121.         }  
  122.     }  
  123.   
  124.   
  125. }  
public class GetJsonActivity extends AppCompatActivity {

    private ListView lv_json_list;
    private List lists=new ArrayList<>();
    private MyAdapter myAdapter;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_json);

        lv_json_list = (ListView) findViewById(R.id.lv_json_list);
        myAdapter = new MyAdapter();
        lv_json_list.setAdapter(myAdapter);

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在拼命loading中...");
    }
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public Object getItem(int i) {
            return lists.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            if(view==null){
                view= LayoutInflater.from(GetJsonActivity.this).inflate(R.layout.item_listview,null);

                ItemTag itemTag=new ItemTag();

                itemTag.tv_content= (TextView) view.findViewById(R.id.tv_item_listview_content);
                itemTag.tv_name= (TextView) view.findViewById(R.id.tv_item_listview_name);
                itemTag.tv_time= (TextView) view.findViewById(R.id.tv_item_listview_time);

                view.setTag(itemTag);
            }

            ItemTag itemTag= (ItemTag) view.getTag();
            itemTag.tv_name.setText(lists.get(i).getName());
            itemTag.tv_content.setText(lists.get(i).getContent());
            itemTag.tv_time.setText(lists.get(i).getTime());

            return view;
        }
    }

    public void getJson(View view){
            new MyTask().execute();
    }

    class MyTask extends AsyncTask{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.show();
        }

        @Override
        protected Object doInBackground(Object[] objects) {
            String path=getString(R.string.server_name)+"fqActiongetJson.action";
            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();
                    //读
                    BufferedReader br=new BufferedReader(new InputStreamReader(is));
                    StringBuffer stringBuffer=new StringBuffer();
                    String str=null;
                    while((str=br.readLine())!=null){
                        stringBuffer.append(str);
                    }
//                    Log.i("test",stringBuffer.toString());

                    //解析JSOn

                    //03.使用FastJson
                    BigFQ bigFQ=JSON.parseObject(stringBuffer.toString(),BigFQ.class);
                    String clazz=bigFQ.getClazz();
                    int num=bigFQ.getLists();

                    Log.i("test","clazz "+clazz+" num:"+num);
                    lists.addAll(bigFQ.getFqs());

                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);

            myAdapter.notifyDataSetChanged();

            progressDialog.cancel();
        }
    }


}





你可能感兴趣的:(Android网络数据Json格式解析)