安卓应用程序中的网络爬取
此示例演示如何在 Android 应用程序中进行网络爬取。
步骤 1 − 在 Android Studio 中创建一个新项目,转到文件 ⇒ 新项目并填写所有必需的详细信息来创建新项目。
步骤 2 – 打开 build.gradle(Mobule:app) 并添加以下依赖项
implementation 'org.jsoup:jsoup:1.11.2'
步骤 3 − 将以下代码添加到 res/layout/activity_main.xml。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:text="" android:layout_width="match_parent" android:layout_height="600dp" android:padding="4dp" android:layout_centerHorizontal="true" /> <Button android:id="@+id/btnView" android:text="Scrap Text from web" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginBottom="25sp" android:layout_alignParentBottom="true"/> </RelativeLayout>
步骤 4 − 将以下代码添加到 src/MainActivity.java
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.btnView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new doIT().execute();
}
});
}
public class doIT extends AsyncTask<Void,Void,Void> {
String words;
@Override
protected Void doInBackground(Void... params) {
try {
Document document = Jsoup.connect("https://tutorialspoint.com/css_online_training/index.asp").get();
words = document.text();
} catch (IOException e) {
e.printStackTrace();
} return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
textView.setText(words);
}
}
}步骤 5 - 将以下代码添加到 androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <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/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>
让我们尝试运行你的应用程序。我假设你已经将你的安卓移动设备与你的电脑连接起来了。要从安卓工作室运行此应用,打开项目的一个活动文件,然后单击工具栏中的运行
图标。选择你的移动设备作为选项,然后检查你的移动设备,它将显示你的默认屏幕 –

单击 此处 下载项目代码。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP