Web Request in Android failing
-
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
-
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
I don't know much about android. But how did you decide that the request is failing? Did you end up with an error? If so what is it, post it here?
If you've never failed... You've never lived...
-
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
Since Android 4.0 (SDK 14), it is no longer allowed to have network communications in the UI thread. The solution is to move the code where you download files into a background thread, for example with a Runnable. Make sure to keep your updateViews.setTextViewText() calls in the UI thread though, with for example a Handler.
-
Since Android 4.0 (SDK 14), it is no longer allowed to have network communications in the UI thread. The solution is to move the code where you download files into a background thread, for example with a Runnable. Make sure to keep your updateViews.setTextViewText() calls in the UI thread though, with for example a Handler.
Thanks a lot! Here is the code I used:
new Thread(new Runnable() {
public void run() {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.google.com/pxi");
ResponseHandler resHandler = new BasicResponseHandler();
String page = "";
try {
page = httpClient.execute(httpGet, resHandler);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final String p = page;
Log.e("response" , page);
//return "Successful!";
test.post(new Runnable() {
public void run() {
test.setText(p);
}
});
}
}).start();Also this page I found helpful: http://developer.android.com/guide/components/processes-and-threads.html[^]