首页 > 解决方案 > 改造 GET 请求仅在 android 通过 USB 电缆插入计算机时有效

问题描述

每当我在将 USB 插入手机的情况下在 android studio 上运行我的应用程序时,我都没有遇到任何问题,并且小部件中填充了正确的 API 数据。但是,当我拔下 USB 并尝试使用小部件时,我没有得到任何数据。事实上,只有时钟更新。我尝试重新启动我的设备,断开并重新连接到互联网,删除小部件并重新创建它,导出到 APK 文件并下载它。

public class MiningRigWidget extends AppWidgetProvider {
    static RemoteViews views;
    static int count = 0;
    static int countSuccess = 0;
    static void updateAppWidget(Context context, final AppWidgetManager appWidgetManager,
                                final int appWidgetId) {
        views = new RemoteViews(context.getPackageName(), R.layout.mining_rig_widget);

        //  on click update
        Intent intentUpdate = new Intent(context, MiningRigWidget.class);
        intentUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        int[] idArray = new int[]{appWidgetId};
        intentUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, idArray);
        PendingIntent pendingUpdate = PendingIntent.getBroadcast(
                context, appWidgetId, intentUpdate,
                PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.last_updated, pendingUpdate);

        String timeString =
                DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());
        views.setTextViewText(R.id.last_updated, "Last Updated: " + timeString);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.ethermine.org/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        final EthermineAPI ethermineAPI = retrofit.create(EthermineAPI.class);
        Call<CurrentStats> call = ethermineAPI.getCurrentStatsData();
        call.enqueue(new Callback<CurrentStats>() {
            @Override
            public void onResponse(Call<CurrentStats> call, Response<CurrentStats> response) {
                if (!response.isSuccessful()) {
                    System.out.println(response.code());
                    return;
                }
                //  view never gets set when unplugged
                countSuccess++;
                views.setTextViewText(R.id.error_log, "Got a successful response " + "(" + countSuccess + ")");
                CurrentStats currentStats = response.body();
                CurrentStats.CurrentStatsData currentStatsData = currentStats.getCurrentStatsData();
                
                Call<Payouts> call2 = ethermineAPI.getCurrentPayoutsData();
                call2.enqueue(new Callback<Payouts>() {
                    @Override
                    public void onResponse(Call<Payouts> call, Response<Payouts> response) {
                        if (!response.isSuccessful()) {
                            System.out.println(response.code());
                            return;
                        }

                        Payouts currentStats = response.body();
                        
                        // set some views based on data here

                        appWidgetManager.updateAppWidget(appWidgetId, views);
                    }

                    @Override
                    public void onFailure(Call<Payouts> call, Throwable t) {
                        System.out.println(t.getMessage());
                    }
                });

                appWidgetManager.updateAppWidget(appWidgetId, views);
            }

            @Override
            public void onFailure(Call<CurrentStats> call, Throwable t) {
                // view never gets set when unplugged
                count++;
                views.setTextViewText(R.id.error_log, t.getMessage() + "(" + count + ")");
                System.out.println(t.getMessage());
            }
        });

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        System.out.println("onUpdate");
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
            Toast.makeText(context, "Widget has been updated! ", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onEnabled(Context context) {
        System.out.println("creating widget");
    }

    @Override
    public void onDisabled(Context context) {
        System.out.println("removing widget");
    }
}

标签: javaandroidretrofit2

解决方案


也许您的小部件没有更新,因为我在该代码中没有看到错误,请尝试仔细阅读小部件的文档或尝试不使用“static void updateAppWidget()”,而使用“public void updateAppWidget()”:https:// developer.android.com/guide/topics/appwidgets

AndroidManifest.xml

<receiver android:name="ExampleAppWidgetProvider" >
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
               android:resource="@xml/example_appwidget_info" />
</receiver>

资源/xml/

  <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
            android:minWidth="40dp"
            android:minHeight="40dp"
            android:updatePeriodMillis="86400000"
            android:previewImage="@drawable/preview"
            android:initialLayout="@layout/example_appwidget"
            android:configure="com.example.android.ExampleAppWidgetConfigure"
            android:resizeMode="horizontal|vertical"
            android:widgetCategory="home_screen">
    </appwidget-provider>

推荐阅读