How to get data from html in android - android

I have get html data from webpage. But i want to get only data excluding html tags.
I have tried this:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(urlText.getText().toString());
// Get the response
BufferedReader rd = new BufferedReader(new InutStreamReader(response.getEntity().getContent()));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = rd.readLine()) != null)
{
textView.append(line);
sb.append(line+"\n");
}
This giving me whole html data. Tell me now i can get data only.

Have you tried using Html.fromHtml(source)? or use any Java HTML parser (If they work on android) for this.
Here source is your html formatted whole data.
EDIT:
while ((line = rd.readLine()) != null)
{
sb.append(line+"\n");
}
String source = sb.toString();
textView.setText(Html.fromHtml(source));
Look at this example Android Parsing HTML Content Containing Links.

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.

android Json parser offline

I'm new to the android world and i have some problem.
I'm developing a project under android and it require a json parser. I get my json file from a web service developed under Zend framework, the link to the web service : "manganew:8080/wsmanganew/manga/manga/idmanga/1" and the content of the json file is
{
"manga": [
{
"idmanga":"1",
"titre":"naruto",
"episode":"145",
"url":"http:\/\/naruto.com\/",
"image":null,
"description":"Naruto Shippuuden .",
"tv":"TV Tokyo",
"dtdebut":"2013-05-23 12:30:00",
"iduser":"1"
}
]}
I'm following this tutorial "http://www.androidhive.info/2012/01/android-json-parsing-tutorial/".
i don't know how index to the web service link in the android, any help will be useful.
thank you
so, what do you need to do? download json object from that url into your app?
that code has method getJSONFromUrl in it's class, you should use it
though, according to comments it has flaws in it.
to read string from file use this code
BufferedReader reader = new BufferedReader(new FileReader("/mnt/sdcard/docs/file.json"));
String line, results = "";
while( (line = reader.readLine()) != null)
results += line;
reader.close();
JSONObject obj = new JSONObject(results);
replace path with path to your file
try this
BufferedReader reader = new BufferedReader(new FileReader("C:\\file.json"));
String line, results = "";
while( (line = reader.readLine()) != null)
results += line;
reader.close();
JSONObject obj = new JSONObject(results);
Replace with your local machine download field path C:\\file.json

Android - Parse text from website

I have webpage with this simple text, which is changeable.
<html><head><style type="text/css"></style></head><body>69766</body></html>
I need parse only number 69766 and save it to variable as String or int. It's possible to parse this number without adding libraries?
Thanks for your questions !
You can do like this
URL url = new URL("http://url for your webpage");
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
StringBuilder builder = new StringBuilder();
while ((inputLine = in.readLine()) != null)
builder.append(inputLine.trim());
in.close();
String htmlPage = builder.toString();
String yourNumber = htmlPage.replaceAll("\\<.*?>","");
For your basic need you should take a lot at Html class.
this link shows how to parse the xml with the SAX parser. Its pretty straight forward.
http://www.codeproject.com/Articles/334859/Parsing-XML-in-Android-with-SAX

Javamail message content newline

I've made an E-Mail Client for my Android Phone using the JavaMail API, and I try to get the message Content with the following Code:
Object contentObject = p.getContent();
InputStream is = (InputStream) contentObject;
reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String everything = sb.toString();
System.out.println(everything);
return everything;
With this method, I get the messsage Content as a String, but without Newlines. How can I format this String that he has the newlines from the Message?
P.S.: This are German e- mails, so the problem may be the encoding!?
Add like that
sb.append(line);
sb.append(System.getProperty("line.separator"));

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