The program will choke at the place of (line = reader.readLine()) != null)

G

gearss8888

I try to use the following method to read pages from Internet, but sometimes the program will choke at the place of (line = reader.readLine()) != null), it tries to read content from internet again and again but still fails to get the line content, then the program stops at this position. How can I solve this problem, if it is possible to use another method the download pages from internet or when the program is choked, if it is possible to stop it and restart the program again?




public String getHTMLResource(String htmlFile) throws IOException {
StringBuilder Content =new StringBuilder();
try {
String line = null;
URL url = new URL(htmlFile);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
Content.append(line+"\n");
}
reader.close();
} catch (Exception e) {}
return Content.toString();
}
 
A

Arne Vajhøj

I try to use the following method to read pages from Internet, but sometimes the program will choke at the place of (line = reader.readLine()) != null), it tries to read content from internet again and again but still fails to get the line content, then the program stops at this position. How can I solve this problem, if it is possible to use another method the download pages from internet or when the program is choked, if it is possible to stop it and restart the program again?

public String getHTMLResource(String htmlFile) throws IOException {
StringBuilder Content =new StringBuilder();
try {
String line = null;
URL url = new URL(htmlFile);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
Content.append(line+"\n");
}
reader.close();
} catch (Exception e) {}
return Content.toString();
}

What does "choke" mean in this context?

You could start by printing the exception instead of
ignoring it!

Arne
 
S

Stefan Ram

the line content, then the program stops at this position.
How can I solve this problem

By investigating whether there are exceptions thrown
and then possibly analyzing those exceptions.
 
L

Lew

I try to use the following method to read pages from Internet, but sometimes the program will choke at the place of (line = reader.readLine()) != null), it tries to read content from internet again and again but still fails to get the line content, then the program stops at this position. How can I solve this problem, if it is possible to use another method the download pages from internet or when the program is choked, if it is possible to stop it and restart the program again?




public String getHTMLResource(String htmlFile) throws IOException {
StringBuilder Content =new StringBuilder();

'content', not 'Content'.
try {
String line = null;
URL url = new URL(htmlFile);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
Content.append(line+"\n");
}
reader.close();
} catch (Exception e) {}
return Content.toString();
}

Since others have addressed your main question, I will only add:

- Don't use TAB characters to indent code in Usenet posts.
- Follow the naming conventions. You were told this in another thread. Why do
you continue to flout them?
- Don't ignore exceptions! (Yes, you were told that before. I'm repeating it.)
- Except for specialized use cases, of which this isn't one, don't catch
'Exception'. Use specific exception types.
- Don't initialize variables to values you won't use.
- Don't declare a method to throw an exception that it cannot possibly throw.

And your program isn't "choking", it's waiting for input.
 
G

gearss8888

Sorry, it is my old program, so the codes still uses the incorrect names for variables, from now on I will use the correct naming rules, I donot flout other people's suggestions.
And your program isn't "choking", it's waiting for input.
I donot know what input the program is waiting for, please explain clearly.
 
R

Roedy Green

public String getHTMLResource(String htmlFile) throws IOException {
StringBuilder Content =3Dnew StringBuilder();
try {
String line =3D null;
URL url =3D new URL(htmlFile);
URLConnection conn =3D url.openConnection();
BufferedReader reader =3D new BufferedReader(new InputStreamRea=
der(conn.getInputStream()));
while ((line =3D reader.readLine()) !=3D null) {
Content.append(line+"\n");
}
reader.close();
} catch (Exception e) {}
return Content.toString();

You can see how I handle this in my http code. See
http://mindprod.com/jgloss/http.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top