• AsyncTask异步交互和httpurlconnection结合使用


    //网络请求数据

    package com.baidu.myutils;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    
    public class MyUtils {
        
        //设置请求方式
        public static String Json_url="http://93.gov.cn/93app/get_search.do?key=%E4%B9%9D%E4%B8%89&startNum=0";
        //判断网络是否连接
        public static boolean Netisavilable(Context context){
            //获得网络管理
            ConnectivityManager cManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            //获得网络详情
            NetworkInfo networkInfo=cManager.getActiveNetworkInfo();
            if(networkInfo==null||!networkInfo.isAvailable()){
                return false;
            }
            return true;
            
        }
        
        public static String check_j(String url){
            String str="";
            StringBuffer sb=new StringBuffer(Json_url);
            try {
                //创建url
                URL myurl=new URL(sb.toString());
                HttpURLConnection urlConnection=(HttpURLConnection) myurl.openConnection();
                urlConnection.setConnectTimeout(5000);
                urlConnection.setReadTimeout(5000);
                if(urlConnection.getResponseCode()==200){
                    BufferedReader br=new BufferedReader
                    (new InputStreamReader
                            (urlConnection.getInputStream(),"utf-8"));
                    
                    
                    str=br.readLine();
                }
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            return str;
        }
    }


    package com.baidu.myutils;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    
    public class MyUtils {
        
        //设置请求方式
        public static String Json_url="http://93.gov.cn/93app/get_search.do?key=%E4%B9%9D%E4%B8%89&startNum=0";
        //判断网络是否连接
        public static boolean Netisavilable(Context context){
            //获得网络管理
            ConnectivityManager cManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            //获得网络详情
            NetworkInfo networkInfo=cManager.getActiveNetworkInfo();
            if(networkInfo==null||!networkInfo.isAvailable()){
                return false;
            }
            return true;
            
        }
        
        public static String check_j(String url){
            String str="";
            StringBuffer sb=new StringBuffer(Json_url);
            try {
                //创建url
                URL myurl=new URL(sb.toString());
                HttpURLConnection urlConnection=(HttpURLConnection) myurl.openConnection();
                urlConnection.setConnectTimeout(5000);
                urlConnection.setReadTimeout(5000);
                if(urlConnection.getResponseCode()==200){
                    BufferedReader br=new BufferedReader
                    (new InputStreamReader
                            (urlConnection.getInputStream(),"utf-8"));
                    
                    
                    str=br.readLine();
                }
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            return str;
        }
    }

    //解析数据  AsyncTask异步交互

    package combaidu.zong;
    
    import java.util.List;
    
    import com.baidu.adapter.MyAdapter;
    import com.baidu.myutils.MyUtils;
    import com.baidu.vo.MyData;
    import com.baidu.vo.MyNews;
    import com.google.gson.Gson;
    
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.app.Activity;
    import android.database.CursorJoiner.Result;
    import android.view.Menu;
    import android.widget.ListView;
    
    public class MainActivity extends Activity {
        // http://93.gov.cn/93app/get_search.do?key=%E4%B9%9D%E4%B8%89&startNum=0
    
        private ListView lv;
        private List<MyData> list;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            init();
        }
        private void init() {
            //找到相应的控件
            lv = (ListView) findViewById(R.id.lv);
            new MyTask().execute(MyUtils.Json_url);
        }
        
        class MyTask extends AsyncTask<String, String , String>{
    
            
            @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
                String str=params[0];
                String jstr=MyUtils.check_j(str);
                
                return jstr;
            }
            @Override
            protected void onPostExecute(String result) {
                System.out.println(result+"--------------");
                
                Gson g=new Gson();
                MyNews myNews=g.fromJson(result, MyNews.class);
                list = myNews.getList();
                System.out.println(list+"-------集合为:-------");
                lv.setAdapter(new MyAdapter(MainActivity.this,list));
                super.onPostExecute(result);
            }
            
        }
    
    }
  • 相关阅读:
    2015年新的征程,我的博客开通啦!
    基于USB3.0、DP1.2的网络隔离数据安全传输方案
    USB OTG to PC USB API简介
    SMA2SATA、PCIe2SATA转换模块(也有叫:Sata Test Fixtures)
    SATA接口硬盘加密器
    SVN二次开发——让SVN、TSVN(TortoiseSVN)支持windows的访问控制模型、NTFS ADS(可选数据流、NTFS的安全属性)
    About USB Data Link Cable API
    蓝牙4.0BLE抓包(三) – 扫描请求和扫描响应
    nRF51822外设应用[2]:GPIOTE的应用-按键检测
    蓝牙4.0BLE抓包(二) – 广播包解析
  • 原文地址:https://www.cnblogs.com/1995yu/p/5448100.html
Copyright © 2020-2023  润新知