Native Mobile App Development With Java

Web Scrapping Example in Android Java

30-Sep-2024

Web Scrapping Example in Android Java

Output:






















1/  Create A New Project: ( Web Scrapping Example )


2/ Add dependency to your build.gralde file ( app module )




implementation("org.jsoup:jsoup:1.15.4")



3/ Add permission to your AndroidManifest.xml file



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




4/  Modify your activity_main.xml



<?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"
android:orientation="vertical"
android:layout_marginTop="50dp"
android:padding="16dp">

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<Button
android:id="@+id/btnScrape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scrape Web Data" />

<TextView
android:id="@+id/tvResult"
android:lineHeight="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Web Data Will Be Displayed Here"
android:paddingTop="20dp" />
<ProgressBar
android:id="@+id/progressbar"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
</LinearLayout>
</ScrollView>

</LinearLayout>



5/  Modify your MainActivity.java



package com.example.webscrappingexample;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {

private TextView tvResult;
private Button btnScrape;
private ProgressBar progressBar;


private Handler mainHandler = new Handler(Looper.getMainLooper());
private ExecutorService executorService = Executors.newSingleThreadExecutor();


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

tvResult = findViewById(R.id.tvResult);
btnScrape = findViewById(R.id.btnScrape);
progressBar = findViewById(R.id.progressbar);


btnScrape.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startWebScraping();
}
});
}

private void startWebScraping() {
progressBar.setVisibility(View.VISIBLE);
executorService.submit(new Runnable() {
@Override
public void run() {
final String resultText = scrapeWebData();

mainHandler.post(new Runnable() {
@Override
public void run() {
progressBar.setVisibility(View.GONE);
tvResult.setText(resultText);
}
});
}
});
}

private String scrapeWebData() {
String resultText = "";
try {
Document doc = Jsoup.connect("https://books.toscrape.com").get();


resultText = doc.text();


} catch (IOException e) {
e.printStackTrace();
resultText = "Error fetching data!";

}

return resultText;
}

@Override
protected void onDestroy() {
super.onDestroy();
executorService.shutdown();
progressBar.setVisibility(View.GONE);

}
}



1/  For web Scrapping I use the website: https://books.toscrape.com/


    if you want to scrap data from other websites you can replace your site URL here:



//previous url
Document doc = Jsoup.connect("https://books.toscrape.com").get();
//replaced url
Document doc = Jsoup.connect("https://your-url.com").get()







Comments