Android Java rxjava 线程抓取网页

Android Java rxjava thread scraping a web page

提问人:Paul 提问时间:5/5/2023 更新时间:5/5/2023 访问量:33

问:

我必须抓取一个网页,我必须获取所有等于的元素并将它们放在 PC 类型的 ArrayList 中,其中它有一些属性。data-component-types-search-result

但是我不知道如何使用 Retrofit 进行此解析。

谁能帮我一把?

// RxJava library and RxAndroid for Android specific bindings
    implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'

    // Retrofit and OkHttp
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.okhttp3:okhttp:3.11.0'

    // RxJava retrofit adapter and Retrofit Gson converter
    implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

Api服务

import io.reactivex.Flowable;
import retrofit2.http.GET;

public interface ApiService {
    @GET("/pc/s?k=pc")
    Flowable<Page> getPc();
}

HttpClient的

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HttpClient {
    private static final String API_BASE_URL = "https://www.amazon.com/";

    private static Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(API_BASE_URL)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    private static ApiService apiService = retrofit.create(ApiService.class);

    public static ApiService getApiService() {
        return apiService;
    }
}

我的小部件

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.RemoteViews;

import io.reactivex.functions.Consumer;

public class MyWidget extends AppWidgetProvider {
    private ApiService apiService = HttpClient.getApiService();

    void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_design);

        //get the widget value
        SharedPreferences preferences = context.getSharedPreferences("PREFS", 0);
        int value = preferences.getInt("value", 1);

        //set the value in the textview
        views.setTextViewText(R.id.text, "" + value);

        //update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);

        //reschedule the widget refresh
        AlarmHandler alarmHandler = new AlarmHandler(context);
        alarmHandler.cancelAlarmManager();
        alarmHandler.setAlarmManager();


//Service http
        apiService.getPc()
                .observeOn(SchedulerProvider.ui())
                .subscribeOn(SchedulerProvider.io())
                .subscribe(new Consumer<Page>() {
                    @Override
                    public void accept(Page page) throws Exception {
                        // Update the UI
                        Log.d("WIDGET2", page.toString());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        // Throw error
                        throwable.printStackTrace();
                    }
                });

        Log.d("WIDGET", "Widget updated!");
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int appWidgetId: appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onDisabled(Context context) {
        //stop updating the widget
        AlarmHandler alarmHandler = new AlarmHandler(context);
        alarmHandler.cancelAlarmManager();

        Log.d("WIDGET", "Widget removed!");
    }

import java.util.ArrayList;

class Pc {

    String name;
    String price;
    boolean sponsored;

    public String getFormattedInfo() {
        StringBuilder builder = new StringBuilder();
        builder.append("Name: " + name + "\n\n");
        builder.append("Price: " + price + "\n");
        builder.append("Sponsored: " + sponsored + "\n");

        return new String(builder);
    }
}

public class Page {

    String numberPc;

    ArrayList<Pc> arrayPc = new ArrayList<Pc>();

}
java android web-scraping 改造 android-networking

评论


答: 暂无答案