Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Web Request in Android failing

Web Request in Android failing

Scheduled Pinned Locked Moved Java
androidcomjsonannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peter Hawke
    wrote on last edited by
    #1

    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
    
    C L 2 Replies Last reply
    0
    • P Peter Hawke

      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
      
      C Offline
      C Offline
      CodingLover
      wrote on last edited by
      #2

      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...

      1 Reply Last reply
      0
      • P Peter Hawke

        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
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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.

        P 1 Reply Last reply
        0
        • L Lost User

          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.

          P Offline
          P Offline
          Peter Hawke
          wrote on last edited by
          #4

          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[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups