Android小部件HttpURLConnection强制关闭

Android小部件HttpURLConnection强制关闭,android,android-widget,httpurlconnection,android-networking,Android,Android Widget,Httpurlconnection,Android Networking,我的小部件应用程序存在强制关闭问题。我读到我可能需要执行一个extendAsyncTask,但我一辈子都不知道如何使用AppWidgetProvider来完成它。我开始使用一个使用不推荐的函数的教程,然后意识到我需要使用HttpURLConnection,现在它强制关闭 这个小部件只是为了让我在工作中关注一个临时工,我绝对不是一个安卓开发者。非常感谢您的帮助 public class BoilerTemp extends AppWidgetProvider { String n

我的小部件应用程序存在强制关闭问题。我读到我可能需要执行一个extend
AsyncTask
,但我一辈子都不知道如何使用
AppWidgetProvider
来完成它。我开始使用一个使用不推荐的函数的教程,然后意识到我需要使用
HttpURLConnection
,现在它强制关闭

这个小部件只是为了让我在工作中关注一个临时工,我绝对不是一个安卓开发者。非常感谢您的帮助

    public class BoilerTemp extends AppWidgetProvider {

    String name;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
        }
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 10000);
        select();
    }

    public void select()
    {
        try{
        URL url = new URL("http://www.someurl.com/temp/select.php");
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestProperty("User-Agent", "");
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
            while ((line = rd.readLine()) != null) {
                name = line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private class MyTime extends TimerTask {
        RemoteViews remoteViews;
        AppWidgetManager appWidgetManager;
        ComponentName thisWidget;
        DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());

        public MyTime(Context context, AppWidgetManager appWidgetManager) {
            this.appWidgetManager = appWidgetManager;
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.boiler_temp);
            thisWidget = new ComponentName(context, BoilerTemp.class);
        }
        @Override
        public void run() {
            remoteViews.setTextViewText(R.id.appwidget_text, "TEMP = " + name + " - " + format.format(new Date()));
            appWidgetManager.updateAppWidget(thisWidget, remoteViews);
        }

    }


    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        CharSequence widgetText = context.getString(R.string.appwidget_text);
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.boiler_temp);
        views.setTextViewText(R.id.appwidget_text, widgetText);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}
有更好的解决方案吗?

试试这段代码(它从URL解析XML)

调用

parser = new XMLParser();
parser.execute(context);
XML解析器类

public class XMLParser extends AsyncTask {

    private Exception exception;

    @Override
    // Parse XML
    protected Object doInBackground(Object[] objects) {
        Context context = (Context) objects[0];

        HttpURLConnection connection;
        InputStream inputStream;
        try {
            URL input = new URL("http://....");
            connection = (HttpURLConnection) input.openConnection();
        } catch (IOException e) {
            exception = e;
            ...
            return context;
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }

        try {

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = factory.newPullParser();

            connection.setRequestMethod("GET");
            connection.setRequestProperty("Accept", "application/xml");
            inputStream = connection.getInputStream();
            parser.setInput(inputStream, "UTF-8");
            ...
    }

    // Parsing done
    protected void onPostExecute(Object val) {
        if (exception != null) {
            sentIntent((Context) val, false);
            exception.printStackTrace();
        } else {
            if (val != null) {
                sentIntent((Context) val, true);
            }
        }
    }

    // Sent intent when parsing done
    private void sentIntent(Context context, final boolean extra) {
        Intent intent = new Intent(context, BoilerTemp.class);
        intent.setAction(Constants.ACTION_XML_PARSED);
        context.sendBroadcast(intent);
    }
}

}

使用LogCat检查与崩溃相关的Java堆栈跟踪:不幸的是,LogCat为空。我正在使用Android Stuido,我终于在LogCat窗口中找到了一些东西。java.lang.RuntimeException:无法启动接收器
public class XMLParser extends AsyncTask {

    private Exception exception;

    @Override
    // Parse XML
    protected Object doInBackground(Object[] objects) {
        Context context = (Context) objects[0];

        HttpURLConnection connection;
        InputStream inputStream;
        try {
            URL input = new URL("http://....");
            connection = (HttpURLConnection) input.openConnection();
        } catch (IOException e) {
            exception = e;
            ...
            return context;
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }

        try {

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = factory.newPullParser();

            connection.setRequestMethod("GET");
            connection.setRequestProperty("Accept", "application/xml");
            inputStream = connection.getInputStream();
            parser.setInput(inputStream, "UTF-8");
            ...
    }

    // Parsing done
    protected void onPostExecute(Object val) {
        if (exception != null) {
            sentIntent((Context) val, false);
            exception.printStackTrace();
        } else {
            if (val != null) {
                sentIntent((Context) val, true);
            }
        }
    }

    // Sent intent when parsing done
    private void sentIntent(Context context, final boolean extra) {
        Intent intent = new Intent(context, BoilerTemp.class);
        intent.setAction(Constants.ACTION_XML_PARSED);
        context.sendBroadcast(intent);
    }
}