访问URL

    URL url;
        try {
            url = new URL(address);
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            
            InputStream input = con.getInputStream();
            //SaxParser(input);
            //SAXPARSER
            
            StringBuilder sb = new StringBuilder();
            byte[] bytes = new byte[1024];
            int n;
            while((n = input.read(bytes,0,bytes.length)) != -1){
                sb.append(new String(bytes, 0, n));
                
            }
            
            
            
            
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

2 HttpGet:

  

HttpGet get = new HttpGet(url);
        HttpClient client = new DefaultHttpClient();
        HttpResponse httpResponse;
        StringBuilder builder = new StringBuilder();
        try {
            httpResponse = client.execute(get);
            HttpEntity httpEntity = httpResponse.getEntity();
            
            InputStream inputStream = httpEntity.getContent();
            //SaxParser(inputStream);
            //DomParser(inputStream);
            PullParser(inputStream);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
原文地址:https://www.cnblogs.com/yk00/p/2891165.html