I have problems with getting the html source code with an url. I am trying to develop an application which takes in an url and upon the button submit by the user, it takes in the url and make calls to the url provided to retrieve the html source code.
I have looked up on various methods of doing it, but have always been presented with errors using the codes provided.
Listed below is one of the codes which I have used but it doesn't seem to work for me, it will throw a Error: null in the exception when I use logcat to debug.
please pardon me if the question sounds simple, I'm new to programming. thanks in advance.
String htmlCode = "";
try {
URL url = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
htmlCode += inputLine;
in.close();
} catch (Exception e) {
Log.d(LOG_TAG, "Error: " + e.getMessage());
}
That code seems to be functional and works for me. Can you post the exception/error you are getting when you run the code?
At a glance, the code looks fine. Make sure you add the Internet permission in your AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET" />
I just ran this code and its working fine. The error must be coming from somewhere else?
Are you running this in the emulator? The emulator has DNS problems sometimes. It's possible the emulator can't find www.google.com, see this post:
Upgraded to SDK 2.3 - now no emulators have connectivity
a background process or use AsyncTask to perform your network transaction on a background thread will be required.
http://developer.android.com/resources/articles/painless-threading.html
Related
This is my very first thread so please bear with me. :)
I want to create an Android Service that searches a specific string on a website. To do this I have tried to download that site and search within the html code but the application always crashes when trying to download it.
Does anybody have any idea how to do this?
Thanks in advance
I had a similer problem when i started making an android app that scans imdb.com for movie information. After a lot of searching the internet and testing things out i came up with this:
import java.net.*;
import java.io.*;
URL url = new URL("websiteToLookAt");
InputStreamReader isr = new InputStreamReader(url.openStream());
BufferedReader bufferedReader = new BufferedReader(isr);
String lineThatIsBeingRead = null;
String theString;
while((lineThatIsBeingRead = bufferedReader.readLine()) != null){
if(lineThatIsBeingRead.contains("StringYouAreLookingFor")){
theString = lineThatIsBeingRead;
break;
}
}
The first line sets up the URL of the website you are scanning
The second line opens a the internet to allow you to access the html source directly
The third line makes a Buffered reader that is able to read the source the InputStreamReader gives it
The fifth line is the string that the current line of HTML source is being held in while the buffered reader is checking if it contains the right string. (string1.contains(string2) looks at wether or not string2 is in string1. example: String myName = "john"; if you were to test if myName.contains("oh"); it would return true)
The Sixth line is the string that you will put the string you are looking for from the HTML source(like if you were looking for the name of a movie, this would be the string you would assign the name to)
The while loop reads the next line of the html source code every time the loop starts over and sets the line it just read to the String variable lineThatIsBeingRead. it will keep doing this as long as there is a new line to read. When the buffered reader comes to the end of the HTML source the conditions for the while loop return false and it breaks the loop.
The if statment checks to see if lineThatIsBeingRead has the string StringYouAreLookingFor in it. if it does, it sets theString(the string you are looking for) to lineThatIsBeingRead(the string that is in the buffered reader) then it breaks the while loop. otherwise, it resets the while loop and it starts all over again.
I have the variable theString in there because i was looking for several strings, but if you only need to find one string you can delete lines 6 & 11 and just have lineThatIsBeingRead as the string you assign the string you are looking for to.
Another thing to keep in mind is java doesn't allow you to connect to the internet through the UI thread(the way java wants you to do it is with an intent so when you publish the app remember to take this out and make it run on an intent). but if you add
if(BuildVERSION.SDK_INT >= 9){
StrickMode.ThreadPolicy Policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(Policy);
}
to the onCreate() method and it will bypass that rule.
Hope this was helpful, and good luck with your project!
i have posted a question for htmlunit in this link: how to use htmlunit with my android project
mainly i have a link, which i have get after login (i have login through web view) this link give me a simple page. in that page there is a textarea and a submit button. and there are some javascript too (i think these javascript run, when i press the submit button). i can do it through webview, but for some reason i don't want to use webview. whene i press submit button, it deliver the value of textarea and some value of hidden field with existing cookies(which are get when i logged in through webview) Post method. i need to do this without webview. now is there any other option beside htmlunit ?? i heard about HttpClient, HttpUrlConnection. but i don't know how to use them to solve my problem, because they are totaly new to me. i think if i use these class i have to run them in a seperate thread from UI tread. one more thing, after submitting it will redirect me to another page. i don't need to do anything with this redirected page.
thank you
this is the same answer which i have given here
i have solve the problem. first of all i was getting the right cookie all time. so what was the problem then. either i was wrong to integrate the cookie with Jsoup or Jsoup was doing something wrong. so, first i have get the page with HttpUrlConnection and then parse it with Jsoup. like this:
URL form = new URL(uri.toString());
HttpUrlConnection connection1 = (HttpURLConnection)form.openConnection();
connection1.setRequestProperty("Cookie", my_cookie);
connection1.setReadTimeout(10000);
StringBuilder whole = new StringBuilder();
BufferedReader in = new BufferedReader(
new InputStreamReader(new BufferedInputStream(connection1.getInputStream())));
String inputLine;
while ((inputLine = in.readLine()) != null)
whole.append(inputLine);
in.close();
Document doc = Jsoup.parse(whole.toString());
any advice about this code would be appreciated.
I have the following code to read a website. The code is searches on google. I want to open automatically the first result and read it again. For the keywords i want the "I am feeling lucky is not working". Please somebody help me find a solution
I have the following code.
URL url = new URL("http://www.google.com/search?q=sample");
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
try {
readStream(in);
finally {
in.close();
}
}
The query sample is just a sample....
Its already discussed in SO QA :
How to search in google by using java code?
Your approach is also a solution. But it will be complex as it requires parsing of returned HTML.
I think there are some APIS for doing this.
http://code.google.com/p/google-api-java-client/
This is deprecated but might still work -->
http://code.google.com/apis/websearch/docs/
This is the scenario.
I do a HTTP POST using the HttpURLConnection class to post to a url say "https://www.abc.com", but this line:
in = new BufferedReader(new InputStreamReader(
httpURLConnector.getInputStream()));
response = in.readLine();
randomly gives me a null response. But if I remove the 's' from the url to become "http://www.abc.com", I get back all responses as expected with no null returns. The weird thing is that it works on the Nexus phone for https but it does not work for the HTC Wildfire. Can someone enlighten me on this? Thanks!
You might want to check the HTTP status code, before trying to read anything. Post some code from the `HttpURLConnector'.
I have a text file on my server. I want to open the text file from my Android App and then display the text in a TextView. I cannot find any examples of how to do a basic connection to a server and feed the data into a String.
Any help you can provide would be appreciated.
Try the following:
try {
// Create a URL for the desired page
URL url = new URL("mysite.com/thefile.txt");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
(taken from Exampledepot: Getting text from URL)
Should work well on Android.
While URL.openStream will work, you would be better off using the Apache HttpClient library that comes with Android for HTTP. Among other reasons, you can use content encoding (gzip) with it, and that will make text file transfers much smaller (better battery life, less net usage) and faster.
There are various ways to use HttpClient, and several helpers exist to wrap things and make it easier. See this post for more details on that: Android project using httpclient --> http.client (apache), post/get method (and note the HttpHelper I included there does use gzip, though not all do).
Also, regardless of what method you use to retrieve the data over HTTP, you'll want to use AysncTask (or Handler) to make sure not to block the UI thread while making the network call.
And note that you should pretty much NEVER just use URL.openStream (without setting some configuration, like timeouts), though many examples show that, because it will block indefinitely if you the server is unavailable (by default, it has no timeout): URL.openStream() Might Leave You Hanging.
Don't forget to add internet permissions to the manifest when taking net resources: (add in manifest).