You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio中用Java为WebView设置外部HTTP链接

Hey there! Let me walk you through how to set up a WebView in Android Studio (using Java) to load that HTTP link to Google. I'll break it down step by step so it's easy to follow.


Step 1: Add WebView to your layout

First, open your activity's layout file (e.g., activity_main.xml) and add a WebView component. Give it an ID so you can reference it in your Java code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
Step 2: Configure WebView in your Activity

Next, open your corresponding Java Activity file (e.g., MainActivity.java). We'll initialize the WebView, set up necessary settings, and load the target URL.

A critical detail here is using a WebViewClient — this stops the link from opening in the system's default browser and keeps it within your app's WebView.

import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webView);

        // Enable JavaScript (optional but recommended for Google's site functionality)
        webView.getSettings().setJavaScriptEnabled(true);

        // Force links to load inside the WebView instead of external browser
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        // Load the target HTTP link
        webView.loadUrl("http://www.google.com");
    }

    // Optional: Let users navigate back through WebView history with the back button
    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}
Step 3: Fix HTTP cleartext traffic block (Android 9+)

Starting from Android 9 (API level 28), the OS blocks cleartext HTTP traffic by default. Since your link uses HTTP (not HTTPS), you need to explicitly allow this in your app's configuration.

Option 1: Quick fix via AndroidManifest.xml

Add android:usesCleartextTraffic="true" to your <application> tag in AndroidManifest.xml:

<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.YourAppName"
    android:usesCleartextTraffic="true"> <!-- Add this line -->
    
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Option 2: More secure network config (advanced)

If you want to restrict cleartext traffic only to specific domains (like google.com), you can create a network security config file. But for simplicity, the first option works perfectly for your use case.

Don't forget the INTERNET permission!

Make sure you've added the internet permission in your AndroidManifest.xml (outside the <application> tag):

<uses-permission android:name="android.permission.INTERNET" />

That's it! Once you implement these steps, your WebView will load the HTTP Google link directly in your app without issues.

内容的提问来源于stack exchange,提问作者Ranjith Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:38:17