यह उदाहरण दर्शाता है कि मैं एंड्रॉइड एप्लिकेशन में वेब स्क्रैपिंग कैसे कर सकता हूं।
चरण 1 - एंड्रॉइड स्टूडियो में एक नया प्रोजेक्ट बनाएं, फाइल ⇒ न्यू प्रोजेक्ट पर जाएं और एक नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2 - बिल्ड.ग्रेडल (मोबाइल:ऐप) खोलें और निम्नलिखित निर्भरता जोड़ें
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://www.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>
आइए आपके एप्लिकेशन को चलाने का प्रयास करते हैं। मुझे लगता है कि आपने अपने वास्तविक Android मोबाइल डिवाइस को अपने कंप्यूटर से कनेक्ट कर लिया है। एंड्रॉइड स्टूडियो से ऐप चलाने के लिए, अपने प्रोजेक्ट की गतिविधि फ़ाइलों में से एक खोलें और टूलबार से रन आइकन पर क्लिक करें। एक विकल्प के रूप में अपने मोबाइल डिवाइस का चयन करें और फिर अपने मोबाइल डिवाइस की जांच करें जो आपकी डिफ़ॉल्ट स्क्रीन प्रदर्शित करेगा -
