Android网络数据Json格式解析

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

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


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

 

  1. public String getJson() throws Exception {  
  2.         // 获取数据  
  3.         // 调用数据库查询数据,返回对象集合(….)  
  4.         List<FQ> fqs = new ArrayList<FQ>();  
  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<FQ> fqs = new ArrayList<FQ>();
        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:

  1. <%@ page language=“java” contentType=“text/plain; charset=utf-8” pageEncoding=“utf-8”%>{fqs}&nbsp;&nbsp;</span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets_01.png"></a></div></div><pre name="code" class="java" style="display: none;">&lt;%@ page language="java" contentType="text/plain; charset=utf-8" pageEncoding="utf-8"%&gt;{fqs}
       struts.xml配置:

    1. <action name=“fqAction*” class=“com.zking.action.FQAcgtion” method=“{1}”>  
    2.             <result name=”dataResult”>/dataResult.jsp</result>    
    3.             <result name=”dataResult”>/dataResultJson.jsp</result>            
    4.         </action>  
    <action name="fqAction*" class="com.zking.action.FQAcgtion" method="{1}">
                <result name="dataResult">/dataResult.jsp</result>  
                <result name="dataResult">/dataResultJson.jsp</result>          
            </action>



        截图如下:






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

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

          activity_get_json.xml

    1. <Button  
    2.         android:layout_width=”match_parent”  
    3.         android:layout_height=”wrap_content”  
    4.         android:text=”获取Json”  
    5.         android:onClick=”getJson”  
    6.         />  
    7.   
    8.     <ListView  
    9.         android:layout_width=”match_parent”  
    10.         android:layout_height=”wrap_content”  
    11.         android:id=”@+id/lv_json_list”  
    12.         >  
    13.     </ListView>  
    <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="获取Json"
            android:onClick="getJson"
            />
    
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lv_json_list"
            >
        </ListView>

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


        1、原生态

         activity_get_json.activity

    1.   
        
    1. <p><span style=“background-color: rgb(240, 240, 240);”>public class GetJsonActivity extends AppCompatActivity {</span></p><p></p>    private ListView lv_json_list;  
    2.     private List<FQ> 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 <jsonArray.length() ; 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<FQ> 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 <jsonArray.length() ; i++) { // JSONObject object=jsonArray.getJSONObject(i); // String name=object.getString("name"); // String content=object.getString("content"); // String time=object.getString("time"); // FQ fq=new FQ(name,content,time); // lists.add(fq); // } // } catch (JSONException e) { // e.printStackTrace(); // } return null; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); myAdapter.notifyDataSetChanged(); progressDialog.cancel(); } } }


          2、GJon解析

    1. public class GetJsonActivity extends AppCompatActivity {  
    2.   
    3.     private ListView lv_json_list;  
    4.     private List<FQ> 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<FQ> 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解析

    1. public class GetJsonActivity extends AppCompatActivity {  
    2.   
    3.     private ListView lv_json_list;  
    4.     private List<FQ> 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<FQ> 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解析数据 JSONJavaScript Object Notation)是一种轻量级的数据交换格式,常用于 Web API 交互。Kotlin 提供了多种解析 JSON 数据的方法,其中和Gson是常见的选择。 阅读详情

相关推荐

Android网络请求解析以及JSON文本

http://blog.csdn.net/qq_34861102/article/details/76762135 这是对资源中描述的博客,有关Android网络请求解析以及JSON文本,现在的资源不知道为什么要积分下载,没有积分的到博客下留一下邮箱就好

ndroid 网络请求json数据解析json数据,生成对应的java bean类一步到位,快速开发

Android 网络请求一般都涉及到图片和JSON数据,怎样快速的请求网络JSON数据解析JSON数据且一步生成自己想要的Java bean实体类?这个涉及到Android 开发效率的问题。由于接触Android 网络这方面比较多,自然就找到一些好的方法来快速开发Android 网络模块的相关内容,接下来就为大家揭晓 一步快速请求,解析JSON 数据生成对应的Java bean实体类的方法。

kite30的博客 833

http请求json解析数据

利用tomcat作为服务器,客户端采用HTTP方式请求json数据进行解析

Android 服务器获取json实例,android客户端从服务器端获取json数据解析的实现代码...

首先客户端从服务器端获取json数据1、利用HttpUrlConnection/*** 从指定的URL中获取数组* @param urlPath* @return* @throws Exception*/public static String readParse(String urlPath) throws Exception {ByteArrayOutputStream outStream =...

weixin_39916360的博客 302

Android网络数据JSON解析使用总结

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。

wenzhi的博客 1万+

Android 网络获取数据Json解析

1.简介JSONJavaScript Object Notation) 是一种轻量级的数据交换格式JSON数据作为目前网络中主流的数据传输格式之一,应用十分广泛。

二进制的博客 1263

Android 网络数据JSON解析使用

json

彬sir哥的博客 953

Android网络编程(四)解析 JSON 格式数据

Android网络编程(四)解析 JSON 格式数据 一,使用JSONObject 解析 JSON 数据也有很多种方法,可以使用官方提供的 JSONObject,也可以使 用谷歌的开源库 GSON。另外,一些第三方的开源库如 Jackson、FastJSON 等也非常不错。 本节中我们就来学习一下前两种解析方式的用法。 修改 MainActivity 中的代码,如下所示: publi

航天飞哥的博客 589

JSON格式数据学习和在Android中用JSONobject和Gson解析网络数据

本人刚入门,有错误请评论指出 一、Json相关知识点 什么是JsonJson是一种存储数据信息的语法格式 Json类似于XML,但是比XML更小、更快、更易解析 Json的语法规则 数据保存为“键/值”对的格式 数据由逗号分隔 花括号保存的是对象 方括号保存的是数据 Json的值可以是以下格式: 数字(整型和浮点型) 字符串(在双引号中) 逻辑值(boolean) 数...

康康 332

Android网络技术(二)——解析XML和JSON格式数据

一、解析XML格式数据 1、准备工作 建立一个本地网页,访问``

干燥的空气,湿润的呼吸 511

android:通过url向网络获取json格式数据解析显示

第一步:通过httpclient向网络获取数据 返回jsonobject格式数据 用到的对象:httpClient联网 httpget or httppost通过地址发出请求  httpresponse 接收idao返回数据 response.getEntity() EntityUtils进行字符串的处理 jsonobject进行数据的封装 第二部:定义方法进行相应的解析  解...

weixin_34122548的博客 1246

Android Studio获取网络JSON数据处理的方法

主要为大家详细介绍了Android Studio获取网络JSON数据处理的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

解析网络json数据demo

解析网络json数据的demo,里面含有gson包,没有用到。

Android访问网络系列之--服务端返回XML或JSON格式数据Android 进行解析显示

例子说明:用户通过访问web资源的最新电影资讯,服务器端生成XML或JSON格式数据,返回Android客户端进行显示。 此案例开发需要两个方面 WEB开发和android开发. 一.web开发相对比较简单,只是模拟一下 相关代码如下: 1.实体Bean package ygc.yxb.domain; /** * 电影资讯实体Bean * @author YXB * *

lynxzong的专栏 9233

Android】使用网络技术——WebView的用法、http协议、OKHttp、解析XML、JSON格式数据笔记整理

新建一个WebView项目

2301_79977698的博客 1566

Android初级开发(九)——网络交互—解析JSON格式数据

一、前言 首先还是在web服务器(我这里用的是tomcat)新建json文本 文本内容 在浏览器上预览一下 二、JSONObject方法1、我们还是在OkHttp的基础上修改代码,布局不变 MainActivity.java代码如下public class MainActivity extends AppCompatActivity { Button sendRequest;

qq_28585471的博客 320

Android】使用网络技术、WebView的用法、OkHttp的用法、解析XML格式数据(Pull,SAX)、解析JSON格式数据JSONObject、GSON)

Android使用网络技术、WebView的用法、OkHttp的用法、解析XML格式数据(Pull,SAX)、解析JSON格式数据JSONObject、GSON)

摸鱼小小虫的博客 591
上一篇: Androidxml的解析
下一篇: Android Get,Post,AsyncHttpClient向服务器提交数据三种方式
梦里梦外不相同
博客等级 码龄10年 20粉丝 14原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值