title: Android: get HTML from web page as String with HttpClient not working
body: <p>I am programming an app that uses HttpClient to connect to a web page (the aim is to be able to copy some of the HTML of a webpage into a String). I tried accomplishing this by using an HttpClient connection. This is the code I used:</p>
  
  <pre><code>public void getText() {
      final TextView contentView = (TextView) findViewById(R.id.textview);
     String url = "http://www.anandtech.com";
      /* An instance of this class will be registered as a JavaScript interface */ 
  HttpClient client = new DefaultHttpClient();
  
  HttpGet get = new HttpGet(url);
  ResponseHandler&lt;String&gt; handler = new BasicResponseHandler();
  try {
      String response = client.execute(get, handler);
      contentView.setText(response);
  } catch (Exception e) {
      // TODO Auto-generated catch block
      contentView.setText(e.getMessage());
  }
  
  }
  </code></pre>
  
  <p>I found the source of the error by removing each statement of this block and then testing if the error still occurs and found that the error originated from the client.execute(get, handler). I have permission to use internet on AndroidManifest.xml, and I know the internet works anyway on the app because a WebView can load just fine. How can I make this work?</p>
  
  <p>Also, I tried to get the String by injecting javascript into a webview url, with the following code: </p>
  
  <p>MyJavaScriptInterface.class
        class MyJavaScriptInterface 
          { 
              private TextView contentView;</p>
  
  <pre><code>        public MyJavaScriptInterface(TextView aContentView)
          {
              contentView = aContentView;
          }
      }
  </code></pre>
  
  <p>MainActivity.java
         @SuppressWarnings("unused") </p>
  
  <pre><code>        public void processContent(String aContent) 
          { 
              final String content = aContent;
              contentView.post(new Runnable() 
              {    
                  public void run() 
                  {          
                      contentView.setText(content);        
                  }     
              });
          } 
      } 
  
      webView.getSettings().setJavaScriptEnabled(true); 
      webView.addJavascriptInterface(new MyJavaScriptInterface(contentView),"INTERFACE"); 
      webView.setWebViewClient(new WebViewClient() { 
          @Override 
          public void onPageFinished(WebView view, String url) {
          webView.loadUrl("javascript:window.INTERFACE.processContent(
          document.getElementsByTagName('p')[0].innerHTML);");
          } 
      }); 
  
      webView.loadUrl("http://tabshowdown.blogspot.com");
  </code></pre>
  
  <p>Both the class and the processContent() method were defined in the Activity's void onCreate method, FIY. In this case the problem is the javascript isn't initiating the processContent() method, since a. If I try to initialize any other javascript command, e.g. document.write(), the webview reacts, so javascript isn't the problem. If I try to initiate the MyJavaScriptInterface and the processContent() method from anywhere else on the script, it works just fine. So it appears to me as if the javascript is having trouble initiating the method (even though other users reported this method to work). </p>
  
  <p>Can someone help me get either of these methods to work?
  Thanks in advance</p>