Android HttpURLConnection application tips

Source: Internet
Author: User

The Android mobile operating system is an open-source operating system that can help us meet many requirements flexibly and conveniently. Here, you can learn about the Android HttpURLConnection application to learn how to read the network, so that you can easily interpret the functions of this system.

There are many examples of Android HttpURLConnection on the Internet. Here we will introduce it to you first.

 
 
  1. void getInput(){   
  2. try  
  3. {  
  4. URL url = new URL("http://www.google.cn/");  
  5. HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  6. conn.setDoInput(true);  
  7. conn.setConnectTimeout(10000);  
  8. conn.setRequestMethod("GET");  
  9. conn.setRequestProperty("accept", "*/*");  
  10. String location = conn.getRequestProperty("location");  
  11. int resCode = conn.getResponseCode();  
  12. conn.connect();  
  13. InputStream stream = conn.getInputStream();  
  14. byte[] data=new byte[102400];  
  15. int length=stream.read(data);  
  16. String str=new String(data,0,length);   
  17. conn.disconnect();  
  18. System.out.println(str);  
  19. stream.close();  
  20. }  
  21. catch(Exception ee)  
  22. {  
  23. System.out.print("ee:"+ee.getMessage());   
  24. }  

You only need to pay attention to configuring a permission. The following nodes should be added to AndroidManifest. xml.

 
 
  1. < /activity> 
  2. < /application> 
  3. < uses-permission android:name="android.permission.INTERNET"> 
  4. < /uses-permission> 
  5. < /manifest>  

You can edit AndroidManifest. xml open with manifest editor to add uses-permission in permissions, then select Android. permission. INTERNET in name, and save it.

The basic application of Android HttpURLConnection is introduced here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.