#New to java, writing a small program. No errors, unexpected behaviour.

43 messages · Page 1 of 1 (latest)

bright adder
#

I'm new to java and made a small app that fetches a stream of strings from a url. The stream of strings is a bunch of JSON objects seperated by a newline. I check if there is new data every second and parse it and do something based on it. The issue is for some reason it only logs the first json string and then a bunch of empty newlines.
When using curl I get data that looks like this: https://pastebin.com/mUPa3LLh
When using my app I get data that looks like this: https://pastebin.com/jywdkDen
and the newlines continue printing every second until the program is killed.
Here is the code itself: https://pastebin.com/3NhRZAjG
I am not sure why it stops, it can fetch data from the URL since it prints the first one but nothing after that.

lucid vaporBOT
#

This post has been reserved for your question.

Hey @bright adder! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

pseudo portal
#

can't really help rn since it's late, but fyi, the 🪜Streams tag is for the streams api, not for I/O streams, those are handled by 💾I/O

bright adder
#

o ok sry

simple stump
#

Is there any need to make it so complicated? What do you obtain when you read the URL simply instead?

bright adder
#

complicated
Probably not but this makes logical sense to me

simple stump
#

Without threads, without available(), without weird parsing, just read the goddamn http response and write it in the console

bright adder
#

its a constant stream

#

I'm confused

simple stump
#

You're confused about what?

simple stump
#

You're confused about the concept of programming without threads?

#

(it never does)

bright adder
#

number of bytes that can be dumped into the byte array?

simple stump
#

We have rules against intervening like that, you know. Passing the buck to you, since you want it

bright adder
#

how should I recieve the HTTP Stream here then?

simple stump
#

Ooops, my bad, looks like here we don't. It's just not exactly smart. Anyway, enjoy

simple stump
bright adder
#

Diffrent question alltogether but

#
import java.net.*;
import java.io.*;

public class URLReader {
    public static void main(String[] args) throws Exception {

        URL oracle = new URL("http://www.oracle.com/");
        BufferedReader in = new BufferedReader(
        new InputStreamReader(oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();
    }
}

If the URL was a constant never ending stream but there were delays in receiving the data would this while loop exit?

simple stump
#

Not normally. An exception could occur if the network connection fails

#

And see, that code, it's simpler already

#

No thread. No available(). No parsing. Simple

bright adder
#

I did the threading and everything since I thought that If there is not a constant stream of data something like this would just exit after the stream stops since the website only sends new data after it receives new data

simple stump
#

Ah if it needs new data then that's not that it will stop, it's that it will block forever. You do need to send the data, but I'm not sure through which mechanism

bright adder
#

but it won't exit?

simple stump
#

(Also constant means never changing. You're meaning neverending)

#

No, it won't exit

bright adder
#

ohh

#

I misunderstood then

simple stump
#

(also could you maybe try instead of asking?)

bright adder
#

i mean this worked until it didn't so I thought I was doing something right

#

it stopped working after I tried adding the config and some other parts

simple stump
#

I can't guarantee your code doesn't work, because I didn't want to try and understand what's the attempt before I knew why it was so complicated

#

I'm merely wondering why is it so complicated, like, is there any benefit to that

bright adder
#

Not really I just didn't know there was a simpler solution available.

bright adder
#

thanks

bright adder