-3

I wan't to display a part of an HTML page, but I only find how to get a source code of an HTML page when I search a solution: How to get the html-source of a page from a html link in android?

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

But if I do that, how can I get a part of the page, and the solution is too old. I've found an another solution named webview, but I don't know what is the best solution to display a part of a html page and how to do it.

EDIT: This page: http://www.solutis.fr/groupe-solutis,mentions-legales.html

Without tag, header, footer, only the content of body without tag too.

Community
  • 1
  • 1
benjyspider
  • 245
  • 2
  • 4
  • 16

1 Answers1

1

What you need is one more line - when you become the HttpResponse it is the whole html from the response site, so you need to remove all tags from it and you could do this with a single line

String responseAsText = android.text.Html.fromHtml(html).toString();

where html is your string with the repsonse from the HttpResponse.

Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • But if i use the prévious code i get deprecated elements like HttpClient, HttpGet, HttpResponse – benjyspider May 27 '15 at 11:37
  • no matter how you get your content of a html page, you can use the line above, but when you don't want to use HttpClient and so on, you could use HttpEntity - take a look here - second provided code fragment from the question's author http://stackoverflow.com/questions/29936731/how-to-get-only-string-from-httpresponse-restapi-android/29937176#29937176 – Gabriella Angelova May 27 '15 at 11:40
  • I don't understand, you're saying that with this line String responseAsText = android.text.Html.fromHtml(html).toString(); I can get the content of my page ? – benjyspider May 27 '15 at 11:41
  • no, you can get it as you want (for example with HttpEntity like in the link I provided you in the comment) and then you can use this line to extract only the text content from the page, without the tags – Gabriella Angelova May 27 '15 at 11:43
  • EntityUtils is deprecated, android change a lot, so a lot of tutorial are deprecated, hod to do with EntityUtils ? Android.com say use: openConnection() but how ? – benjyspider May 27 '15 at 11:53
  • Try with this code: `DefaultHttpClient client = new MyHttpClient(getApplicationContext());` `HttpGet get = new HttpGet("http://www.solutis.fr/groupe-solutis,mentions-legales.html");` `// Execute the GET call and obtain the response` `HttpResponse getResponse = client.execute(get);` `HttpEntity responseEntity = getResponse.getEntity();` `// Retrieve a String from the response entity` `String content = EntityUtils.toString(responseEntity);` or something like this - try to Google it http://stackoverflow.com/questions/9679673/how-do-i-use-the-command-httpentity – Gabriella Angelova May 27 '15 at 11:55
  • I dont see where he put the url here: http://stackoverflow.com/questions/29936731/how-to-get-only-string-from-httpresponse-restapi-android/29937176#29937176 in public String readResponse(HttpResponse res) – benjyspider May 27 '15 at 12:20
  • the url is set to the HttpGet or HttpPost, the code is from the response above – Gabriella Angelova May 27 '15 at 12:39
  • I have to use the two codes ? Its hard to nderstand now... I don't see how to implement in my onCreateView – benjyspider May 27 '15 at 12:57
  • this is the whole code http://pastebin.com/rG7eha7s you should write in your onCreateView – Gabriella Angelova May 27 '15 at 13:01
  • defaulthttpclient httpGet httpResponse and httpentity are deprecated, i think its because i use API 14 no ? – benjyspider May 27 '15 at 13:06
  • hmmmm another possible solution is to use HttpUrlConnection or OkHttp – Gabriella Angelova May 27 '15 at 13:11
  • Its look hard i was looking here: http://stackoverflow.com/questions/29225654/android-http-get-request But the modifications to do looks hard to me, i think theyre a better solution like JSOUP... – benjyspider May 27 '15 at 13:18
  • try with JSOUP if you want, another HTTP library is Volley https://developer.android.com/training/volley/index.html – Gabriella Angelova May 27 '15 at 13:22
  • What is for you the simplest solution using API 14 ? – benjyspider May 27 '15 at 13:22
  • maybe the volley library something like this http://pastebin.com/FE83ztak of course with your url – Gabriella Angelova May 27 '15 at 13:26