Loading following URL with BufferedReader, but content is not delivered. Even though a plain browser can show content. So str will remain nil. Any idea why?
URL url = new URL("http://www.omdbapi.com/?t=zorr&y=&plot=short&r=json");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {}
Log.d("alma", str);
You are ignoring all of the lines that you are reading. You then exit the loop when str becomes null. So, your Log.d() call will always show null.
If you want to use the lines that you are reading, use str inside` your currently empty block:
while ((str = in.readLine()) != null) {
// do something with str
}
You might also wish to consider using a third-party library that offers a simpler API. OkHttp3, for example, makes getting a string response from a URL fairly easy.
try this:
URL url = new URL("http://www.omdbapi.com/?t=zorr&y=&plot=short&r=json");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
Log.d("alma", str); // this should be here
}
Related
I develop a google glass app using mirror api. during development I used "Introspected tunnels to localhost" to receive the notification.
Now I uploaded my app on production server. So now I configure my callback URL as my domain name like https://www.mydomain.com:8443/notify. But I get empty notification.
in notify servlet:
BufferedReader notificationReader = new BufferedReader(
new InputStreamReader(request.getInputStream()));
String notificationString = "";
int lines = 0;
while (notificationReader.ready()) {
notificationString += notificationReader.readLine();
lines++;
if (lines > 1000) {
throw new IOException(
"Attempted to parse notification payload that was unexpectedly long.");
}
}
LOG.info("\ngot raw notification : " + notificationString);
in catalina.out
Feb 13, 2014 12:51:48 PM com.google.glassware.NotifyServlet doPost
INFO: got raw notification :
How can I solve it?
StringBuffer stringBuffer = new StringBuffer();
String line = "";
BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(request.getInputStream()));
while ((line = bufferReader.readLine()) != null) {
stringBuffer.append(line);
}
notificationString = stringBuffer.toString();
Hope it will works.
I think you should use readLine() method.One of the answer from stack overflow suggest not using ready() for such requirements.
The ready method tells us if the Stream is ready to be read.
Imagine your stream is reading data from a network socket. In this
case, the stream may not have ended, because the socket has not been
closed, yet it may not be ready for the next chunk of data, because
the other end of the socket has not pushed any more data.
In the above scenario, we cannot read any more data until the remote
end pushes it, so we have to wait for the data to become available, or
for the socket to be closed. The ready() method tells us when the data
is available.
I had this same problem and I changed the code to look like this:
StringBuffer jb = new StringBuffer();
String notificationString = "";
String line = "";
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
jb.append(line);
}
notificationString = jb.toString();
Try This One:
while(notificationReader.ready()) {
notificationString = notificationString.concat(notificationReader.readLine());
lines++;
}
i want decode some content of an .eml-Mail-File. The File contains Strings like "Gesch=C3=A4ftsbedingungen", it shoult be > "Geschäftsbedingungen"
reader = new InputStreamReader(new FileInputStream(path));
BufferedReader in = new BufferedReader(reader);
while ((line = in.readLine()) != null)
{
System.out.println(line);
}
Im not sure how i do that. I try to use "MimeUtility", but i do not get along so.
MimeUtility test;
System.out.println(test.decode(line));
Note : MimeUtility is an utility class so it contains all static methods so you should use
System.out.println(MimeUtility.decode(line));
Though I think you should use MimeUtility.decodeText or MimeUtility.decodeWord method or commons MimeUtility.decodeText method.
I have the following code. What I would like to do is read each line from the BufferedReader directly into a StringBuffer to reduce memory overhead. Once it gets to the end of the data stream I would like it to exit the while loop.
StringBuffer line = new StringBuffer();
URL url = new URL("a url");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
int count = 0;
while(line.append(reader.readLine()) != null){
System.out.println(line.toString());
line.delete(0,line.length());
}
It reads the stream fine but when I get to the end of the stream it returns null and keeps printing null without exiting the loop. Any
This while(line.append(reader.readLine()) != null) is basically the same as saying while(line.append(reader.readLine()).toString() != null) which is never likely to happen.
The other problem you might have, is null is actually being translated to a literal String of "null". That's why it's printing "null", the value isn't actually null - confused yet...
Instead, try something like...
String text = null;
while((text = reader.readLine()) != null){
line.append(text)
System.out.println(line.toString());
line.delete(0,line.length());
}
Updated
While I'm here, I might suggest that you are actually not saving your self anything.
readLine will create String object, which you're putting into a StringBuffer. You're not actually saving any memory, but rather complicating the process.
If you're really worried about creating lots of String objects in memory, then use BufferedReader#read(char[]) instead. Append the resulting character array to the StringBuffer.
Also, unless you need synchronized access to the StringBuffer, use StringBuilder instead, it's faster.
This works perfectly. You just have to catch the NUllPointerException
while(line.append(reader.readLine().toString()) != null){
You could try the same with this for-loop:
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line); // Or whatever
}
I am doing my first Android app and I have to take the code of a html page.
Actually I am doing this:
private class NetworkOperation extends AsyncTask<Void, Void, String > {
protected String doInBackground(Void... params) {
try {
URL oracle = new URL("http://www.nationalleague.ch/NL/fr/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
String s1 = "";
while ((inputLine = in.readLine()) != null)
s1 = s1 + inputLine;
in.close();
//return
return s1;
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
but the problem is it takes too much time. How to take for exemple the HTML from the line 200 to the line 300 ?
Sorry for my bad english :$
Best case use instead of readLine() use read(char[] cbuf, int off, int len). Another dirty way
int i =0;
while(while ((inputLine = in.readLine()) != null)
i++;
if(i>200 || i<300 )
DO SOMETHING
in.close();)
You get the HTML document through HTTP. HTTP usually relies on TCP. So... you can't just "skip lines"! The server will always try to send you all data preceding the portion of your interest, and your side of communication must acknowledge the reception of such data.
Do not read line by line [use read(char[] cbuf, int off, int len)]
Do not concat Strings [use a StringBuilder]
Open The buffered reader (much like you already do):
URL oracle = new URL("http://www.nationalleague.ch/NL/fr/");
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
Instead of reading line by line, read in a char[] (I would use one of size about 8192)
and than use a StringBuilder to append all the read chars.
Reading secific lines of HTML-source seams a little risky because formatting of the source code of the HTML page may change.
Hi guys so this is an exert from the code I have
public ItemList() throws Exception {
//itemList = new List<Item>() ;
List<Item> itemList = new ArrayList<Item>() ;
URL itemPhrases = new URL("http://dl.dropbox.com/u/18678304/2011/BSc2/phrases.txt"); // Initilize URL
BufferedReader in = new BufferedReader(new InputStreamReader(
itemPhrases.openStream())); // opens Stream from html
while ((inputLine = in.readLine()) != null) {
inputLine = in.readLine();
System.out.println(inputLine);
Item x = new Item(inputLine);
itemList.add(x);
} // validates and reads in list of phrases
for(Item item: itemList){
System.out.println(item.getItem());
}
in.close();// ends input stream
}
My problem is that I am trying to read in a list of phrases from the URL http://dl.dropbox.com/u/18678304/2011/BSc2/phrases.txt but when it prints out what I have collected it just prints:
aaa
bbb
ddd
I have tried researching the library and using the debugger but neither have helped.
You should remove inputLine = in.readLine(); from inside the while loop, it calls readLine() function a second time, thus skipping every second line.
Your loop should look like this:
while ((inputLine = in.readLine()) != null) {
//this line must not be here inputLine = in.readLine();
System.out.println(inputLine);
Item x = new Item(inputLine);
itemList.add(x);
}
You are calling in.readLine() twice in a row. Remove the second call.