-1

I am currently working on an android app that takes the contents of a requested website and searches it for certain words. I am unsure though how I can convert the InputStream into a String. I'm a pretty unexperienced so I guess my code is a little ugly:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private AppBarConfiguration mAppBarConfiguration;
String EanCode;
String FddbData;
InputStream stream;

public void Urlconnect() {                                                                   

    final Thread ConnectThread = new Thread(new Runnable(){
        public void run(){
            try {
                URL url = new URL("https://fddb.info/db/de/suche/?udd=0&cat=site-de&search=" + EanCode);
                URLConnection connection = url.openConnection();
                InputStream stream = (InputStream) connection.getContent();
                StreamToString();


            } catch (Exception e){
                e.printStackTrace();
            }
        }
    });
    ConnectThread.start();

}

2:

    public void StreamToString() throws IOException {

    Scanner scanner = new Scanner(stream);
    StringBuffer sb = new StringBuffer();

    while(scanner.hasNext()){
        sb.append(scanner.toString());
    }

    Logger logger = Logger.getLogger(MainActivity.class.getName());
    logger.info(sb.toString() + "bananeneis");

    FddbData = sb.toString();
}

}

This is the Error I am getting:

W/System.err:     at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at java.util.Scanner.<init>(Scanner.java:568)
    at com.example.prepper.MainActivity.StreamToString(MainActivity.java:22)
    at com.example.prepper.MainActivity$4.run(MainActivity.java:9)
    at java.lang.Thread.run(Thread.java:764)

Any Help is appreciated!

EDIT 2: Solved by removing InputStream in

 URLConnection connection = url.openConnection();
 InputStream stream = connection.getContent();
 StreamToString();
  • 1
    My guess would be that the field `stream` is `null`. – Turing85 Aug 30 '20 at 12:13
  • @Turing85 No, Stream is not null. That's what I put the logger there for. Also i checked that other question thread before and it didn't help me ._. – Wombat_Rancher Aug 30 '20 at 12:27
  • At `InputStream stream = (InputStream) connection.getContent();` you are declaring and initializing *local* variable named `stream`. Is it intentional? Just asking since in your `StreamToString()` method you seem to be using `stream` *field* (those are not same variables despite having same name which means that `stream` *field* probably still holds `null`). – Pshemo Aug 30 '20 at 12:30
  • The `logger`-part is never reached since the `Exception` is thrown before it is reached. Thus, these statements cannot provide any information. --- Please read: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Turing85 Aug 30 '20 at 12:33
  • Java programming conventions have methods, fields, and variables start with a lower case letter (streamToString). – NomadMaker Aug 30 '20 at 13:06

2 Answers2

0

You need to pass your stream to StreamToString. At the moment that method is operating on a different, unrelated variable that’s confusingly also called stream.

  1. Change

    public void StreamToString() throws IOException {
    

    to

    public void StreamToString(final InputStream stream) throws IOException {
    
  2. Change

    StreamToString();
    

    to

    String result = StreamToString(stream);
    
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
-1

Try to make InputStream stream; as global variable. I can't see the close() method so insert it after StreamToString(); method to close a opened stream, or insert a close() after while loop. Good luck :)

Kris
  • 13
  • 4