Android java 小部件调用 http 错误 android.os.NetworkOnMainThreadException [duplicate]

Android java widget call http error android.os.NetworkOnMainThreadException [duplicate]

提问人:Paul 提问时间:5/5/2023 最后编辑:Paul 更新时间:5/5/2023 访问量:162

问:

我遇到以下错误:

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 http 小部件 android-networking

评论

0赞 Paul 5/5/2023
@Selvin:太旧了,没有更新的信息。

答:

0赞 jayesh gurudayalani 5/5/2023 #1

根据错误,看起来您正在主线程上调用 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

评论

1赞 Paul 5/5/2023
这是 kotlin 代码吗?
0赞 jayesh gurudayalani 5/5/2023
是的,但您可以使用另一种机制来代替协程,该机制在 IO 线程上执行 API 调用代码,而不是主线程,因为您可以在 Kotlin 中使用协程
1赞 Paul 5/5/2023
由于我没有在项目中使用 kotlin,因此由于 AsyncTask 已被弃用,我可以在 java for android 中使用什么来做同样的事情。
0赞 jayesh gurudayalani 5/5/2023
您可以使用 RXJava,它允许您轻松切换线程以及丰富的运算符。它还允许您管理/取消您的任务 digitalocean.com/community/tutorials/android-rxjava-retrofit
0赞 Paul 5/5/2023
如果我使用这样的东西,那就不好了:?Thread thread = new Thread(new Runnable(){
0赞 ahvroyal 5/5/2023 #2

您不应该在 android 应用程序中的主线程上进行联网,而是考虑使用 AsyncTask 进行网络操作,在其中它将工作卸载到主线程之外。

AsyncTask 参考手册: https://developer.android.com/reference/android/os/AsyncTask

评论

0赞 Paul 5/5/2023
此类在 API 级别 30 中已弃用。
0赞 ahvroyal 5/5/2023
这是最简单的方法,但是您可以使用其他选项;我可以向您建议的是为此目的使用 OkHttp 库。OkHttp 库指南:guides.codepath.com/android/Using-OkHttp 请注意,现代首选方法是使用 kotlin 协程,但这只是 kotlin。