Code piece:
HttpURLConnection url = new HttpURLConnection(new URL("myurl"));
InputStream connInputStream = null;
try
{
connInputStream = conn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(connInputStream));
boolean matchFound = false;
String strLine = in.readLine();
}
...
In this code piece I get strLine = null, Which means in.readLine is null.
What are the possible reasons that in.readline can come as null.
This code piece does not throw any null pointer exception.
Can this case arise because of the time lag in reading url source to the stream ?
There is only one reason: you've reached the end of the underlying stream. What the end of stream is depends on the underlying stream implementation.
Related
I have a problem with reading a ULR response. On Android it only reads around the half of the response.
If I use the same code in a normal Java project everything works fine.
try {
String _output = null;
URL url = new URL("http://example.com");
BufferedReader buffer = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder everything = new StringBuilder();
String line;
while ((line = buffer.readLine()) != null) {
everything.append(line);
}
_output = everything.toString();
buffer.close();
System.out.print(_output);
} catch (IOException e) {
e.printStackTrace();
}
How do you know that it's only half of the response? If you rely on what is printed with System.out.println() then you should be aware that Logcat has a limitation that prevents it from printing more than 4,000 characters. Anything after that is truncated. To check how much of the response you have, you could print everything.length()first, ot see if you are in that situation.
You can look at this existing question on SO for reference, but there are many others.
I am currently trying to read a String from a BufferedReader but cant find a way to do this...
Of course I tried
BufferedReader inStream = null;
inStream = new BufferedReader(new InputStreamReader(client.getInputStream()));
String test = inStream.readLine();
However the result turns out as null when trying to print to a screen even though the BufferedReader inStream is equal to some kind of message.
Based on the documentation, the BufferedReader.readLine() returns null only when the end of the stream is reached. This means if the first call to readLine() returns null, there was nothing in the input stream to begin with.
So this is a very simple problem with a simple solution that I'm just not seeing:
I'm trying to get a list of data through an InputStream, looping until I reach the end of the stream. On each iteration, I print the next line of text being passed through the InputStream. I have it working but for one small problem: I'm truncating the first character of each line.
Here's the code:
while (dataInputStream.read() >= 0) {
System.out.printf("%s\n", dataInputReader.readLine());
}
And the output:
classpath
project
est.txt
Now, I know what's going on here: the read() call in my while loop is taking the first char on each line, so when the line gets passed into the loop, that char is missing. The problem is, I can't figure out how to set up a loop to prevent that.
I think I just need a new set of eyes on this.
readLine for DataInputStream is deprecated. You may try wrapping it with a BufferedReader:
try
{
String line;
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( dataInputStream ) );
while( (line = bufferedReader.readLine()) != null )
{
System.out.printf("%s\n", line);
}
}
catch( IOException e )
{
System.err.println( "Error: " + e );
}
Also, I`m not sure, that it is a good idea to use available() due to this specification:
* <p>Note that this method provides such a weak guarantee that it is not very useful in
* practice.
Use one BufferedReader and InputStreamReader, here is one example:
InputStream in=...;
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while (br.ready()) {
String line = br.readLine();
}
dataInputStream.read() reads the first character of the InputStream, the same as dataInputReader.readLine() reads the complete next line. Every read character or line is then gone. you can use the dataInputStream.available() to check if the InputStream has data available.
That should print the correct output:
while (dataInputStream.available()) {
System.out.printf("%s", dataInputReader.read());
}
String line;
while ((line = dataInputReader.readLine()) != null) {
System.out.println(line);
}
Hello i am trying to read a URL feed with an interval and store it as an object. However when i try to run it, it gives me this error [Fatal Error] :1:1: Premature end of file.
Here is the code:
thisUrl is a static http address
url = new URL(thisUrl);
URLstream = url.openStream();
ir = new InputStreamReader(URLstream);
buff = new BufferedReader(ir);
String xObject = "";
while (buff.ready()) {
String temp = buff.readLine();
xObject += temp;
}
After using the stream i will close it
URLstream.close();
ir.close();
buff.close();
Quoting the docs for the return type and value of the ready() method
True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
Your next read() may well block on the stream. That does not mean you are done reading the stream. Use this instead.
String temp = null;
while ( (temp = buff.readLine()) != null) {
xObject += temp;
}
I have a piece of code that reads the content from a non-empty InputStream. However, it works fine in Eclipse and using ant script in my computer, but it fails in an another computer, the result is an empty String, I have checked, the the InputStream is not null. The inputstream is reading a local file, and the file is not empty.
Here are the two different ways I have tried, both of them return an empty String:
Way 1:
StringBuilder aStringBuilder = new StringBuilder();
String strLine = null;
BufferedReader aBufferedReaders = new BufferedReader(new InputStreamReader(anInputStream, "UTF-8"));
while ((strLine = aBufferedReaders.readLine()) != null)
{
aStringBuilder.append(strLine);
}
return aStringBuilder.toString()
Way 2:
StringBuffer buffer = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = theInputStream.read(b)) != -1;)
{
buffer.append(new String(b, 0, n));
}
String str = buffer.toString();
return str;
Thanks in advance!
The input stream can be non-null but still empty - and if no exceptions are being thrown but an empty string is being returned, then the input stream is empty. You should look at the code which is opening the input stream in the first place - the code to read from the stream isn't the source of the error, although you need to decide which encoding you're trying to read, and use that appropriately. (The first code looks better to me, explicitly using UTF-8 and using an InputStreamReader for text conversion.)