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);
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);
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?
source share