Android App Widget - AsyncTask API in update

I want to create a simple widget on my Android Homescreen, where I can fill in my zip code or city, and having presented this data, I would like to be able to update the Widget with data from an API call in Openweathermap.org.

I took every step to make it work, but for some reason, Widget textView will not update the collected data.

This is my main activity.

public class WeerManWidget extends AppWidgetProvider { public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget"; static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { CharSequence widgetText = WeerManWidgetConfigureActivity.loadTitlePref(context, appWidgetId); // Construct the RemoteViews object RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.weer_man_widget); views.setTextViewText(R.id.appwidget_text, widgetText); Intent configIntent = new Intent(context, WeerManWidgetConfigureActivity.class); configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); PendingIntent configPendingIntent = PendingIntent.getActivity(context, appWidgetId, configIntent, 0); views.setOnClickPendingIntent(R.id.btnSettings, configPendingIntent); configIntent.setAction(ACTION_WIDGET_CONFIGURE + Integer.toString(appWidgetId)); new GetWeatherTask(views).execute(widgetText.toString()); // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // There may be multiple widgets active, so update all of them for (int appWidgetId : appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId); } } @Override public void onDeleted(Context context, int[] appWidgetIds) { // When the user deletes the widget, delete the preference associated with it. for (int appWidgetId : appWidgetIds) { WeerManWidgetConfigureActivity.deleteTitlePref(context, appWidgetId); } } @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 } } 

And this is the class that I use to call the API.

 public class GetWeatherTask extends AsyncTask<String, String, String> { private RemoteViews views; GetWeatherTask(RemoteViews views) { this.views = views; } @Override public String doInBackground(String... params) { String postalCode = params[0]; HttpURLConnection urlConnection = null; URL url = null; JSONObject object = null; JSONArray myArray = null; InputStream inStream = null; try { url = new URL("http://api.openweathermap.org/data/2.5/weather?q="+postalCode+",nl&appid=XXXXX&units=metric"); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.connect(); inStream = urlConnection.getInputStream(); BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream)); String temp, response = ""; while ((temp = bReader.readLine()) != null) { response += temp; } object = (JSONObject) new JSONTokener(response).nextValue(); JSONObject obj = object.getJSONObject("main"); String weatherTemp = obj.getString("temp"); double weatherFloat = Double.parseDouble(weatherTemp); //weatherFloat = (weatherFloat - 273.15); String newTemp = String.valueOf(weatherFloat); return newTemp; } catch (Exception e) { return e.toString(); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException ignored) { } } if (urlConnection != null) { urlConnection.disconnect(); } } } @Override public void onPostExecute(String result) { Log.v("WeerManWidget", result); views.setTextViewText(R.id.appwidget_text, result); } } 

In onPostExecute mode in AsyncTask, I log the results. It has the correct data. So everything is going well, but when I want to update the view using setTextViewText, nothing happens. I am new to Android development and don't know.

Does anyone want to enlighten me?

+5
source share

All Articles