Android访问网页的两种方式

目录

两种打开网页的简单方式

访问网页是APP最常用的功能,正如大家所知,加载和显示网页通常都是由浏览器来完成的,所以打开网页的最简单的方法就是调用手机系统浏览器。然而大部分时候用户的需求里明确指出,不允许打开系统浏览器。Android考虑到这种需求,提供了一个WebView控件,省去了重新编写一个浏览器的麻烦。先说明下这两种打开方式:


(一)调用浏览器显示

1.调用系统默认浏览器访问

隐式调用Intent,指定Intent的action是Intent.ACTION_VIEW;

Intent intent =new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http://www.baidu.com");
intent.setData(url);
startActivity(intent);

2.调用指定浏览器访问

调用指定的浏览器(前提得安装在设备上^-^),需要用intent.setClassName()来指定packgename和主启动activity:

Intent intent =new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
startActivity(intent);

目前的浏览器不说多如牛毛,常见的也有不少了,每个人的偏好肯定都不一样,需要启用其他的浏览器的话,只需要修改以下相应的packagename和主启动activity就能够调用其他浏览器

intent.setClassName("packagename","主启动activity");

这里先出一些常见浏览器的包名和主活动参数:

uc浏览器":"com.uc.browser", "com.uc.browser.ActivityUpdate“
opera浏览器:"com.opera.mini.android", "com.opera.mini.android.Browser"
qq浏览器:"com.tencent.mtt", "com.tencent.mtt.MainActivity"

(二)在Activity中直接访问(WebView显示)

1.在AndroidManifest.xml文件里添加网络访问权限

磨刀不误砍柴工,准备工作需要做好,我们使用WebView的最终目的是打开一个网页,而android中访问网络是需要声明权限的,所以我们需要修改一下AndroidManifest.xml文件,加入权限声明:

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

2.在布局文件里添加一个WebView控件

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

3.在Activity中实例化WebView对象并实现访问网页

WebView webView = (WebView) findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(newWebViewClient());
webView.loadUrl("http://www.baidu.com")

首先使用findViewById()获取WebView的实例,在调用WebView的getSettings()方法设置浏览器的属性,上面的代码只是简单调用了setJavaScriptEnabled()方法让WebView支持JavaScript脚本。接着调用setWebViewClient()方法传入一个WebViewClient实例,这段代码保证了在网页跳转中仍然让内容出现在我们的WebView控件中,而不是打开系统的浏览器。最后调用loadUrl()并传入网址,就能将相应的网页展示出来了。

后记:关于WebView的属性的详细解析过段时间再贴出来,需要再好好研究。。

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Android Studio 中访问没有 SSL 证书的网页,可以通过以下两种方法: 1. 忽略证书验证 在求中忽略证书验证是一种不安全的做法,不建议在生产环境中使用。但在测试和开发环境中,可以通过以下方式实现: ```java try { // 创建信任所有证书的 SSLContext 对象 SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }}, new SecureRandom()); // 创建忽略证书验证的 OkHttpClient 对象 OkHttpClient client = new OkHttpClient.Builder() .sslSocketFactory(sslContext.getSocketFactory()) .hostnameVerifier((hostname, session) -> true) .build(); // 构建求 Request request = new Request.Builder() .url("https://example.com") .build(); // 发送求 Response response = client.newCall(request).execute(); String responseBody = response.body().string(); } catch (Exception e) { e.printStackTrace(); } ``` 2. 安装自签名证书 如果你有自己的证书,可以将其安装到 Android 设备中,使设备信任该证书。步骤如下: 1. 将证书文件添加到项目的 assets 目录中。 2. 在应用启动时,将证书文件复制到设备的私有目录中,例如 /data/data/com.example.app/files/。 3. 创建信任该证书的 SSLContext 对象,并使用该 SSLContext 创建 OkHttpClient 对象。 4. 构建求并发送求。 以下是示例代码: ```java try { // 将证书文件从 assets 目录拷贝到应用私有目录中 File certFile = new File(getFilesDir(), "example.crt"); if (!certFile.exists()) { InputStream inputStream = getAssets().open("example.crt"); FileOutputStream outputStream = new FileOutputStream(certFile); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } outputStream.flush(); outputStream.close(); inputStream.close(); } // 加载证书文件 CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); InputStream inputStream = new FileInputStream(certFile); X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(inputStream); // 创建信任该证书的 SSLContext 对象 KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(null, null); keyStore.setCertificateEntry("example", certificate); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keyStore); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, trustManagerFactory.getTrustManagers(), null); // 创建 OkHttpClient 对象 OkHttpClient client = new OkHttpClient.Builder() .sslSocketFactory(sslContext.getSocketFactory()) .build(); // 构建求 Request request = new Request.Builder() .url("https://example.com") .build(); // 发送求 Response response = client.newCall(request).execute(); String responseBody = response.body().string(); } catch (Exception e) { e.printStackTrace(); } ``` 注意:以上代码只适用于自签名证书,如果是公开的证书,应该使用系统默认的 SSLContext 对象和 OkHttpClient 对象进行求。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值