Парсинг сайтов: Урок 3

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
private Document doc;
private Thread secThread;
private Runnable runnable;
private ListView listView;
private CustomArrayAdapter adapter;
private List<ListItemClass> arrayList;

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





}
private void init()
{
listView = findViewById(R.id.listView);
arrayList = new ArrayList<>();
adapter = new CustomArrayAdapter(this,R.layout.list_item_1,arrayList,getLayoutInflater());
listView.setAdapter(adapter);
runnable = new Runnable() {
@Override
public void run() {
getWeb();

}
};
secThread = new Thread(runnable);
secThread.start();
ListItemClass items = new ListItemClass();
items.setData_1("Dollar");
items.setData_2("213");
items.setData_3("657");
items.setData_4("78");
arrayList.add(items);
items = new ListItemClass();
items.setData_1("Dollar");
items.setData_2("98");
items.setData_3("6");
items.setData_4("456");
arrayList.add(items);
adapter.notifyDataSetChanged();






}
private void getWeb()
{
try {
doc = Jsoup.connect("https://minfin.com.ua/currency/").get();
Elements tables = doc.getElementsByTag("tbody");
Element our_table = tables.get(0);
Elements elements_from_table = our_table.children();
Element dollar = elements_from_table.get(0);
Elements dollar_elements = dollar.children();
Log.d("MyLog","Tbody size : " +dollar_elements.get(1).text() );





} catch (IOException e) {
e.printStackTrace();
}
}
}

ListItemClass.java

public class ListItemClass {
String data_1;
String data_2;
String data_3;
String data_4;

public String getData_1() {
return data_1;
}

public void setData_1(String data_1) {
this.data_1 = data_1;
}

public String getData_2() {
return data_2;
}

public void setData_2(String data_2) {
this.data_2 = data_2;
}

public String getData_3() {
return data_3;
}

public void setData_3(String data_3) {
this.data_3 = data_3;
}

public String getData_4() {
return data_4;
}

public void setData_4(String data_4) {
this.data_4 = data_4;
}
}

CustomArrayAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class CustomArrayAdapter extends ArrayAdapter<ListItemClass> {
private LayoutInflater inflater;
private List<ListItemClass> listItem = new ArrayList<>();



public CustomArrayAdapter(@NonNull Context context, int resource, List<ListItemClass> listItem, LayoutInflater inflater) {
super(context, resource, listItem);
this.inflater = inflater;
this.listItem = listItem;


}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
ListItemClass listItemMain = listItem.get(position);
if(convertView == null){
convertView = inflater.inflate(R.layout.list_item_1, null, false);
viewHolder = new ViewHolder();
viewHolder.data1 = convertView.findViewById(R.id.tvData1);
viewHolder.data2 = convertView.findViewById(R.id.tvData2);
viewHolder.data3 = convertView.findViewById(R.id.tvData3);
viewHolder.data4 = convertView.findViewById(R.id.tvData4);
convertView.setTag(viewHolder);



}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.data1.setText(listItemMain.getData_1());
viewHolder.data2.setText(listItemMain.getData_2());
viewHolder.data3.setText(listItemMain.getData_3());
viewHolder.data4.setText(listItemMain.getData_4());








return convertView;
}
private class ViewHolder{
TextView data1;
TextView data2;
TextView data3;
TextView data4;


}
}

build.gradle (Module:App)

apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

defaultConfig {
applicationId "drt.nece_desvan.cursapp"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'org.jsoup:jsoup:1.13.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

list_item_1.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="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp">

<TextView
android:id="@+id/tvData1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textStyle="bold" />

<TextView
android:id="@+id/tvData2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView" />

<TextView
android:id="@+id/tvData3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView" />

<TextView
android:id="@+id/tvData4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView" />
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Курс валют в Украине"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.161"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.054" />

<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

5 комментариев для “Парсинг сайтов: Урок 3”

  1. У меня не подключаются:
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;

    Из-за этого компиляция не проходит.
    Причина в чем может быть?

  2. Перезагрузил Studio и компиляция прошла!
    Вот только сейчас на этапе исполнения из-за ошибки вылетает.
    Завтра попробую разобраться.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *