最近利用下班时间,找了看什么书比较适合初学android的朋友,很多人推荐了这本书,于是就买了一本,感觉看书,思考,动手,再思考和总结这样过程还是很有必要的,于是就打算把自己学习的东西简单的总结一下;方便自己以后查找,也有利于学习的巩固。在这里首先要感谢一下书籍的作者——郭霖前辈。
Android App如果没有网络的支持,就会显得非常的单薄,本地的资源相较于互联网上的丰富世界差太远了。现在还有很多数据处理都是放在云端处理,本地只负责提供数据和接受数据,说到这里,又想起来研究生时期老师让我们研究的课题了,就是均衡本地与云端数据处理。想想还是蛮怀恋的。好了,不多说了;关于Android网络这一块,我也会就和书本给出以下几块内容------WebView基本使用,HttpURLConnection基本使用,OkHttp基本使用,XML文件两种解析方式(Pull和Sax),JSON解析相关以及最后的综合示例代码。本文示例代码连接
1,HttpURLConnecction简介
从它的名字就可以很清晰的看出,它是建立Http通讯的API,之前还有一个HttpClient,不过现在基本上被弃用了。其实在使用WebView的时候,它也是对网络访问的过程进行了封装(发送请求,接受响应,解析数据等操作);下面就来看看通过手动完成这一系列操作该怎么实现。
2,示例代码
MainAcctivity.java代码
package com.hfut.operationhttpconnection;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
EditText url;
TextView webContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
private void initUI() {
url=findViewById(R.id.web_url);
webContent=findViewById(R.id.display_webContent);
}
public void querySourceCode(View view){
new Thread(new Runnable() {
@Override
public void run() {
BufferedReader reader=null;
HttpURLConnection connection=null;
try {
URL tempUrl = new URL(url.getText().toString());
try {
connection= (HttpURLConnection) tempUrl.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
int backCode=connection.getResponseCode();
System.out.println("backCode:"+backCode);
InputStream in = connection.getInputStream();
reader=new BufferedReader(new InputStreamReader(in));
if(backCode==200) {
String tempLine;
StringBuilder stringBuilder=new StringBuilder();
while((tempLine=reader.readLine())!=null){
stringBuilder.append(tempLine);
}
//Android不允许在子线程中修改UI
//webContent.setText(stringBuilder.toString());
showResult(stringBuilder.toString());
}
else{
Log.i(TAG, "run:网络错误 ");
}
} catch (IOException e) {
e.printStackTrace();
}
//关闭数据流
finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(connection!=null){
connection.disconnect();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
).start();
}
private void showResult(final String s) {
runOnUiThread(new Runnable() {
@Override
public void run() {
webContent.setText(s);
}
});
}
}
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:orientation="vertical"
tools:context="com.hfut.operationhttpconnection.MainActivity">
<EditText
android:id="@+id/web_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:hint="请在此处输入要查看源码的网址"
android:textSize="20dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="querySourceCode"
android:layout_marginTop="10dp"
android:text="查看网页源码"
android:textSize="20dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/display_webContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="此处显示网页源码内容" />
</ScrollView>
</LinearLayout>
AndroidManifest.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hfut.operationhttpconnection">
<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,运行结果
第一步:运行程序
第二步:输入正确网址,点击“查看网页源码”按钮
总结:关于HttpURLConnection的基本使用就简单介绍一下,因为以后使用的话,更多的是使用开源的项目。当然里面的原理都是一样的。它的其他方法或者使用技巧,自己可以结合兴趣研究一下。下一部分,我们会使用OkHttp这个开源项目来实现同样的效果;随便还可以学习一下如何在自己的项目中引入优秀的开源项目。本文示例代码链接
注:欢迎扫码关注


本文介绍了一个简单的Android应用案例,该应用使用HttpURLConnection获取并显示网页源代码。文章提供了完整的代码示例,包括MainActivity.java、activity_main.xml和AndroidManifest.xml文件,并展示了如何运行该程序。
1977

被折叠的 条评论
为什么被折叠?



