最近利用下班时间,找了看什么书比较适合初学android的朋友,很多人推荐了这本书,于是就买了一本,感觉看书,思考,动手,再思考和总结这样过程还是很有必要的,于是就打算把自己学习的东西简单的总结一下;方便自己以后查找,也有利于学习的巩固。在这里首先要感谢一下书籍的作者——郭霖前辈。
Android App如果没有网络的支持,就会显得非常的单薄,本地的资源相较于互联网上的丰富世界差太远了。现在还有很多数据处理都是放在云端处理,本地只负责提供数据和接受数据,说到这里,又想起来研究生时期老师让我们研究的课题了,就是均衡本地与云端数据处理。想想还是蛮怀恋的。好了,不多说了;关于Android网络这一块,我也会就和书本给出以下几块内容------WebView基本使用,HttpURLConnection基本使用,OkHttp基本使用,XML文件两种解析方式(Pull和Sax),JSON解析相关以及最后的综合示例代码。本文示例代码链接
1,WebView简介
主要用于在自己的App中展示网页,不是在浏览器中哟。关于WebView的继承关系和详细内容,请点击查看;它的基本用法也很简单,下面在代码中我就会它在App中展示一个网页和通过隐式Intent在系统浏览器打开一个网页比较一下,随便复习一下之前的知识。关于WebView的使用,这里不展开。
2,示例代码
MainActivity.java代码:
package com.hfut.operationwebview;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.show_web);
}
/**
* 通过浏览器展示网页
*/
public void openWebByBrowser(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://blog.csdn.net/hfut_why"));
//社区官网代码
// Uri uri = Uri.parse("https://www.example.com");
// Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
/**
* 通过WebView控件展示网页
*/
public void openWebByWebView(View view) {
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://blog.csdn.net/hfut_why");
}
}
layout_main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hfut.operationwebview.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:onClick="openWebByBrowser"
android:text="通过系统浏览器打开一个网页"
android:textSize="20dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:onClick="openWebByWebView"
android:text="通过WebView控件打开一个网页"
android:textSize="20dp" />
<WebView
android:id="@+id/show_web"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
主配置文件AndroidManifest.xml文件代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hfut.operationwebview">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<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/AppTheme">
<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>
3,运行结果
总结:这部分内容介绍的较少,主要就是关于WebView控件的一个引入介绍。运行结果最后一步主要是体现我在代码中关于WebView中的一个属性设置。本文示例代码链接
注:欢迎扫码关注