open a url without help of android browser in android? - android

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?

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();

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

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);

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.example.com/" + URL);
response = httpClient.execute(httpGet, localContext);

Related

Parse HTML text in Android

I'm trying to parse some HTML in my Android app and I need to get the text:
Pan Artesano Elaborado por Panadería La Constancia. ¡Esta Buenísimo!
in
Is there any easy way to get only the text and remove all html tags?
The behavior that I need is exactly the one shown in this PHP code http://php.net/manual/es/function.strip-tags.php
Document doc = Jsoup.parse(html);
Element content = doc.getElementById("someid");
Elements p= content.getElementsByTag("p");
String pConcatenated="";
for (Element x: p) {
pConcatenated+= x.text();
}
System.out.println(pConcatenated);//sometext another p tag
Well when you want just to show it, then webview would help you, just set that string to webview and you got it.
When you would to use it elsewhere then i am to stupid for that :D.
String data = "your html here";
WebView webview= (WebView)this.findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadDataWithBaseURL("", data, "text/html", "UTF-8", "");
also you can pass just web URL webview.loadDataWithBaseURL("url","","text/html", "UTF-8", "");
Firstly get HTML code with
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
then I recommend to create custom tag in HTML such as <toAndroid></toAndroid> and then you can get text with
String result = html.substring(html.indexOf("<toAndroid>", html.indexOf("</toAndroid>")));
your html for example
<toAndroid>Hello world!</toAndroid>
will result
Hello world!
Note that you can place <p> into <toAndroid> tags and then remove it in Java from result.

How to login and keep cookie for later use of a webpage

So, I have this webpage which I want to access, but first I have to login from another webpage. I want to keep the cookies and then use it for later automatic login. So far what I did:
First, this is the login webpage: https://autenticacao.uvanet.br/autenticacao/pages/login.jsf
It's my university's student's area.
public class Consulta extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
StringBuilder builder = new StringBuilder(100000);
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(urls[0]);
try {
List<NameValuePair> val = new ArrayList<NameValuePair>(2);
val.add(new BasicNameValuePair("form:usuario", "myusername"));
val.add(new BasicNameValuePair("form:senha", "mypass"));
httpPost.setEntity(new UrlEncodedFormEntity(val));
HttpResponse response = client.execute(httpPost);
InputStream content = response.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
builder.append(s);
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
}
This is the class I use to make the HttpPost and this is how I call it:
#Override
public void onClick(View v) {
try{
String html = new Consulta().execute("https://autenticacao.uvanet.br/autenticacao/pages/login.jsf").get();
Document doc = Jsoup.parse(html);
Element link = doc.select("title").first();
String t = link.text();
tv1.setText(t);
}catch(Exception e){
e.printStackTrace();
}
}
I believed it would work this way:
I send the webpage to login to Consulta.java
The class would get the fields "form:usuario" and "form:senha" and fill them with myusername and mypassword and then login
The class would return me html code of the second webpage as string
But what happens is that it returns me the first webpage (the login one). I'm pretty sure I'm doing something wrong, but I don't know what, could someone help me? Also, sorry for my english, it's not my main language.
When you do the login (in https://autenticacao.uvanet.br/autenticacao/pages/login.jsf), I don't think the response is the html code of the second webpage. Are you sure about this?
I think the normal behavior for a login page is to respond with the same page (the login one) but adding the session cookie and the header to do a redirect to the second webpage, but not the second page itself.
In this case, you have to read the http header response to extract these parameters: the cookies and the URL of the second webpage.
Using the object HttpResponse:
Header[] h = response.getAllHeaders();
But I recommend you to use HttpURLConnection class instead of DefaultHttpClient.

How to make post requests with webview?

I want to make an http post request using webview.
webView.setWebViewClient(new WebViewClient(){
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
webView.postUrl(Base_Url, postData.getBytes());
return true;
}
});
The above code snippet loads the webpage. I want to access the response of this request.
How can i obtain the response of an http post request using webview?
Thanks in Advance
First add the support of the http library to your gradle file:
To be able to use
useLibrary 'org.apache.http.legacy'
After this you can use the following code to perform post request in your webview:
public void postUrl (String url, byte[] postData)
String postData = "submit=1&id=236";
webview.postUrl("http://www.belencruzz.com/exampleURL",EncodingUtils.getBytes(postData, "BASE64"));
http://belencruz.com/2012/12/do-post-request-on-a-webview-in-android/
The WebView does not let you access the content of the HTTP response.
You have to use HttpClient for that, and then forward the content to the view by using the function loadDataWithBaseUrl and specifying the base url so that the user can use the webview to continue navigating in the website.
Example:
// Executing POST request
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(postContent);
HttpResponse response = httpclient.execute(httppost);
// Get the response content
String line = "";
StringBuilder contentBuilder = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
while ((line = rd.readLine()) != null) {
contentBuilder.append(line);
}
String content = contentBuilder.toString();
// Do whatever you want with the content
// Show the web page
webView.loadDataWithBaseURL(url, content, "text/html", "UTF-8", null);

Get HTML source from URL

I have this URL, http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FINR
which returns the current USD->INR conversion rate as text.
the text displayed on the screen is the only text in the HTML source of the page.
I was struggling to get that HTML source as many things I tried returned Exceptions.
for e.g.:
public static String getHtml(String url)
throws ClientProtocolException, IOException
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
result = reader.readLine();
return result;
}
returned an exception when called from
try {
String test = getHtml("google.com/ig/calculator?hl=en&q=1USD%3D%3FINR");
Log.d("ASDADS", test);
} catch (Exception e) {
Log.d("ASDASD", "FAILED");
}
Also, I have added the INTERNET permission in Manifest, and my network is also working without fault.
Make your URL something like,
"http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FINR"
Also the result from it is in JSON not in HTML.. So parse the result like JSON..

How to programmatically download an HTML page in Android and get its HTML?

I need to download an HTML page programmatically and then get its HTML. I am mainly concerned with the downloading of the page. If I download the page, where will I put it?
Will I have to keep in an String variable? If yes then how?
This site provides a good explanation on how to download a file, and also how to set the location to where it should be stored. You do not have to, and should not, keep it in a string variable. If you are to manipulate the data I would suggest you use an XML parser.
You can call this method in doInBackground of AsyncTask
String html = "";
String url = "ENTER URL TO DOWNLOAD";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();

Categories

Resources