Here is my onUpdate code.
ComponentName thisWidget = new ComponentName(context,
MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
final int N = appWidgetIds.length;
for ( int ii = 0; ii < N ; ii++)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Toast.makeText(context, "Loading", Toast.LENGTH_SHORT).show();
remoteViews.setTextViewText(R.id.update, "Loading2");
try {
data = connect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
now = Calendar.getInstance();
timeHour = now.get(Calendar.HOUR_OF_DAY);
remoteViews.setTextViewText(R.id.update, "The latest PSI reading is " + data);
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(allWidgetIds, remoteViews);
}
}
And here is the method for connect();
public String connect() throws IOException
{ String temp = "WAD";
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://app2.nea.gov.sg/anti-pollution-radiation-protection/air-pollution/psi/psi-readings-over-the-last-24-hours");
HttpResponse response;
response = client.execute(get);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line+"\n");
}
in.close();
temp = sb.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return temp;
}
The thing is my code seems to die at
response = client.execute(get);
According to what LogCat was telling me. It pointed out the tag "System.err" at that line, and at data=connect();
The connect() method worked well on other apps, not widgets, so I have no idea what I'm doing wrong.
EDIT:
Here is the logcat.
06-27 21:30:38.058: W/System.err(28060): at com.example.psi.MyWidgetProvider.connect(MyWidgetProvider.java:284) 06-27 21:30:38.058: W/System.err(28060): at com.example.psi.MyWidgetProvider.onUpdate(MyWidgetProvider.java:71) 06-27 21:30:38.058: W/System.err(28060): at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)