1

I am developing an android application in which I want to fetch the news from a url without opening default browser of android. This means I want to fetch only the texutal content, only news instead of complete html page in browser. How can I do this?

Verbeia
  • 4,400
  • 2
  • 23
  • 44
user898115
  • 11
  • 2

4 Answers4

3

If I understand correctly - you need to make a request online and receive in return the html code. This is done as follows:

DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(http://example.com/news));

HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line;
while ((line = in.readLine()) != null) {
    sb.append(line);
}
in.close();

String html = sb.toString();
Udo Held
  • 12,314
  • 11
  • 67
  • 93
Anton
  • 921
  • 3
  • 12
  • 26
0
    Hi Yes You can implement this.Use This code which i mention below.



 - WebView webView = (WebView) findViewById(R.id.webview_btn);
   WebSettings settings = webView.getSettings();
   mProgress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");

               settings.setJavaScriptEnabled(true);
               settings.setSupportZoom(true);
               settings.setBuiltInZoomControls(true);

               WebViewClient client = new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    // TODO Auto-generated method stub
                    view.loadUrl(url);
                    mProgress.show();

                    return true;
                }
                   @Override
                   public void onPageFinished(WebView view, String url) {

                    if(mProgress.isShowing())
                    {
                        mProgress.dismiss();
                    }
                   }
               };
               webView.loadUrl(enter your url);
               webView.setWebViewClient(client);
Rishabh Agrawal
  • 861
  • 2
  • 15
  • 25
0
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.example.com/" + URL);
response = httpClient.execute(httpGet, localContext);
0

Do you mean that you want to parse the actual content of the webpage to your application? When I did so in one of my apps, I parsed the whole webpage with a simple http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html and then I took out those tags which where relevant. This however requires some pretty heavy dynamical programming running under an asynctask (http://developer.android.com/reference/android/os/AsyncTask.html).

URL url = new URL(XML_INIT_ADRESS);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(url.openStream(), null);

I'm personally not very experienced with Android yet and I'm still learning but you should be able to parse the news from a webpage this way.

Edit: This approach pretty much requires some kind of identification of the certain "news-tags", Antons answer is better if they are "undefinable".

Johan S
  • 3,531
  • 6
  • 35
  • 63