1

I was wondering if is any way to get HTML code from any url and save that code as String in code? I have a method:

private String getHtmlData(Context context, String data){
    String head = "<head><style>@font-face {font-family: 'verdana';src: url('file://"+ context.getFilesDir().getAbsolutePath()+ "/verdana.ttf');}body {font-family: 'verdana';}</style></head>";
    String htmlData= "<html>"+head+"<body>"+data+"</body></html>" ;
    return htmlData;
 }

and I want to get this "data" from url. How I can do that?

3 Answers 3

2

Try this (wrote it from the hand)

URL google = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(google.openStream()));
String input;
StringBuffer stringBuffer = new StringBuffer();
while ((input = in.readLine()) != null)
{
    stringBuffer.append(input);
}
in.close();
String htmlData = stringBuffer.toString();
Sign up to request clarification or add additional context in comments.

6 Comments

I get 06-10 10:43:45.094: E/AndroidRuntime(7210): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fontfordroid/com.example.fontfordroid.MainActivity}: android.os.NetworkOnMainThreadException
did you give your app internet permission?
its strange, I just tested it by myself and it works fine! Else take a look here: stackoverflow.com/questions/2423498/…
You paste this code in Thread or AsyncTask? I paste this code in onCreate.
From API LEVEL 11, you cannot perform network related task on main thread, you have to do it in another thread. Otherwise you will get an exception "android.os.NetworkOnMainThreadException".
|
1

Sure you can. That's actually the response body. You can get it like this:

HttpResponse response = client.execute(post);
String htmlPage = EntityUtils.toString(response.getEntity(), "ISO-8859-1");

Comments

0

take a look at this please, any other parser will work too, or you can even make your own checking the strings and retrieving just the part you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.