Okay so I have an app widget and I am trying to make a web request and It just keeps failing. Here is the code:
public class IDEAAllotmentTrackerActivity extends AppWidgetProvider {
/** Called when the activity is first created. */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// To prevent any ANR timeouts, we perform the update in a service
context.startService(new Intent(context, UpdateService.class));
}
public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
// Build the widget update for today
RemoteViews updateViews = null;
try {
updateViews = buildUpdate(this);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Build an update that holds the updated widget contents
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this, IDEAAllotmentTrackerActivity.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
/\*\*
\* Build a widget update to show the current Wiktionary
\* "Word of the day." Will block until the online API returns.
\* @throws IOException
\* @throws ClientProtocolException
\*/
public RemoteViews buildUpdate(Context context) throws ClientProtocolException, IOException{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet("http://www.google.com"));
StatusLine statusLine = response.getStatusLine();
String pageContent = "";
if(statusLine.getStatusCode() == HttpStatus.SC\_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
pageContent = out.toString();
//..more logic
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
RemoteViews update