Android widget调用http报错android.os.NetworkOnMainThreadException

问题描述 投票:0回答:2

我有以下错误:

FATAL EXCEPTION: main
Process: com.name.app, PID: 21535
java.lang.RuntimeException: Unable to start receiver com.name.app.MyWidget: 
android.os.NetworkOnMainThreadException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:4012)
at android.app.ActivityThread.access$1500(ActivityThread.java:232)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2025)
....

我需要每隔一定时间制作一个小部件

http
调用网站进行抓取。

我有以下代码:

package com.name.app;

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

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

import java.io.IOException;

public class MyWidget extends AppWidgetProvider {

    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();

        Document doc;

        try {
            // fetching the target website
            doc = Jsoup.connect("https://pageWeb...").get();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        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!");
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AmazonWidget"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyWidget"
            android:exported="true">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>

        <receiver
            android:name=".WidgetService"
            android:enabled="true" />
    </application>

</manifest>

进入应用程序的权限信息,它告诉我不需要权限。

你能告诉我哪里错了吗?

java android http widget android-networking
2个回答
0
投票

根据错误,您似乎正在主线程上调用 api,这是不允许的,因此我们需要将您的 api 调用转移到 IO 线程。你能试试下面提到的解决方案

来自

try {
            // fetching the target website
            doc = Jsoup.connect("https://pageWeb...").get();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

val job = CoroutineScope(Dispatchers.IO).launch {
            try {
                // fetching the target website
                doc = Jsoup.connect("https://pageWeb...").get();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        job.cancel()

job 对象你需要将它声明为成员并且

job.cancel()
需要放在
onDisabled
方法中


0
投票

您不应该在 Android 应用程序的主线程上进行网络连接,而是考虑使用 AsyncTask 进行网络操作,在该操作中它将工作从主线程中卸载出来。

异步任务参考: https://developer.android.com/reference/android/os/AsyncTask

© www.soinside.com 2019 - 2024. All rights reserved.