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.
- void getInput(){
- try
- {
- URL url = new URL("http://www.google.cn/");
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setDoInput(true);
- conn.setConnectTimeout(10000);
- conn.setRequestMethod("GET");
- conn.setRequestProperty("accept", "*/*");
- String location = conn.getRequestProperty("location");
- int resCode = conn.getResponseCode();
- conn.connect();
- InputStream stream = conn.getInputStream();
- byte[] data=new byte[102400];
- int length=stream.read(data);
- String str=new String(data,0,length);
- conn.disconnect();
- System.out.println(str);
- stream.close();
- }
- catch(Exception ee)
- {
- System.out.print("ee:"+ee.getMessage());
- }
- }
You only need to pay attention to configuring a permission. The following nodes should be added to AndroidManifest. xml.
- < /activity>
- < /application>
- < uses-permission android:name="android.permission.INTERNET">
- < /uses-permission>
- < /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.