IT月光

导航

HttpURLConnection网络请求

//创建访问的方法
    public String Check_json(){
        //创建一个结果字符串
        String result="";
        //拼接字符串
        StringBuffer sb=new StringBuffer(PICTURE_URL);
        //创建Url
        try {
            URL url=new URL(sb.toString());
            //打开连接
            HttpURLConnection connection;
            try {
                connection = (HttpURLConnection) url.openConnection();
                //设置连接的时间
            connection.setConnectTimeout(5*1000);
            connection.setReadTimeout(5*1000);
            if(connection.getResponseCode()==200){
                BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
                //读取每行
                result=br.readLine();
                
            }
            } catch (IOException e1) {
                
                e1.printStackTrace();
            }
        
        } catch (MalformedURLException e) {
            
            e.printStackTrace();
        }
        return result;
        
    }
    
    

 

posted on 2016-04-29 21:15  IT月光  阅读(371)  评论(0编辑  收藏  举报