URL类的使用


前言

记录每天学习的内容方便以后自己复习回顾(第一天)。


以下是本篇文章正文内容。

一、URL类的使用?

示例:URL是互联网上“资源”的唯一标识,通常URL由协议名、主机、端口和资源组成,格式组成如下:
protocol://host:port/resourceName

二、使用步骤

1.编写activity_main.xml文件

代码如下(示例):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请输入网址:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/et_url"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="20sp" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="确定"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

2.编写MainActivity文件

代码如下(示例):

package com.hngy.zp.day07_02_http_url;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
//Android4.0后就不能在主线程访问网络,请在android2.3以下运行
//必须在androidManifest.xml文件中添加网络权限 <uses-permission android:name="android.permission.INTERNET" />
public class MainActivity extends AppCompatActivity {
    
    
    TextView tvContent;
    EditText etUrl;
    Button btnOk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        btnOk.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                String inUlr = etUrl.getText().toString().trim();
                if (inUlr == null || inUlr == "") {
    
    
                    Toast.makeText(getApplicationContext(), "请输入网址", Toast.LENGTH_SHORT).show();
                    return;
                }
                try {
    
    
                    URL url = new URL(inUlr);
                    //获得url的协议名
                    String protocol = "协议名" + url.getProtocol() + "\n";
                    //获得url的主机名
                    String host = "主机名" + url.getHost() + "\n";
                    //获得url的端口号
                    String port = "端口号" + url.getPort() + "\n";
                    String strResult = protocol + host + protocol;
                    //显示内容
                    tvContent.setText(strResult);
                    //构造BufferReader对象使用缓冲
                    InputStream inputStream = url.openStream();
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                    BufferedReader reader = new BufferedReader(inputStreamReader);
                    String s;
                    //从输入流一直读取数据,直到读完为止
                    while ((s = reader.readLine()) != null) {
    
    
                        strResult += s;
                    }
                    //读完关闭输入流显示内容
                    inputStream.close();
                    reader.close();
                    tvContent.setText(strResult);
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }
            }
        });
    }

    private void init() {
    
    
        etUrl = findViewById(R.id.et_url);
        btnOk = findViewById(R.id.btn_ok);
        tvContent = findViewById(R.id.tv_content);
    }
}

3.编写AndroidManifest.xml文件

代码如下(示例):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hngy.zp.day07_02_http_url">

    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Day07_02_http_url">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

总结

这里对文章进行总结:
以上就是今天要讲的内容,本文仅仅简单介绍了URL的使用,本程序得到的是该网页的html源码,并未对其进行解析,如果在程序中加入解析将会呈现漂亮的网页,Android4.0后就不能在主线程访问网络,请在android2.3以下运行,必须在androidManifest.xml文件中添加网络权限 。

猜你喜欢

转载自blog.csdn.net/qq_41935662/article/details/115169500