码迷,mamicode.com
首页 > Web开发 > 详细

HttpURLConnection网络请求

时间:2016-04-29 22:10:09      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

//创建访问的方法
    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;
        
    }
    
    

 

HttpURLConnection网络请求

标签:

原文地址:http://www.cnblogs.com/1995yu/p/5447591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!