0

I am having a force close issue with my widget app. I read that I might need to do an extend AsyncTask but cannot for the life of me figure out how to do that with AppWidgetProvider. I started using a tutorial that used deprecated functions, then realized I needed to use HttpURLConnection, now it force closes.

This widget is just for me to keep an eye on a boilers temp at work and I am by no means an android developer lol. Any help is seriously appreciated.

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

update I found this code made the app work, but while being seriously frown upon.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Any better solutions?

Jptalon
  • 99
  • 2
  • 10
  • 1
    Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Nov 01 '15 at 23:09
  • Unfortunately LogCat is empty. I am using Android Stuido – Jptalon Nov 01 '15 at 23:14
  • I finally got something in the LogCat window. java.lang.RuntimeException: Unable to start receiver – Jptalon Nov 01 '15 at 23:24

1 Answers1

0

Try this code (it parses XML from URL).

Invocation

parser = new XMLParser();
parser.execute(context);

XML Parser class

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

}

Joss
  • 106
  • 1
  • 2
  • 8