Eldar Azulay
Eldar Azulay

Reputation: 271

Can't save results of BufferedReader.readLine() to String

I can log the result of BufferedReader.readLine() with Log.e("debug", e.getMessage()), but can't assign the result to a String. Why is that?

new Thread() {
    @Override
    public void run() {
        try {
            //Connecting
            Log.i(debugString, "Attempting to connect to server");
            socket = new Socket(hostname, portnumber);
            Log.i(debugString, "Connection established!");

            //receive message from server
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            Log.e(debugString, br.readLine());  // This works, but really...
            String s = br.readLine(); // ...this is how I *want* to save it.
        } catch (IOException e) {
            Log.e(debugString, e.getMessage());
        }
    }
}.start();

Upvotes: 0

Views: 244

Answers (1)

Lew Bloch
Lew Bloch

Reputation: 3433

You haven't shown any code where you try to display the result from the readLine() call. If you want the result to display in your app, you have to display the result in your app. You assign the value read to a variable s (not a good name, btw), then silently drop the reference, thus losing the returned value before you ever display it.

Also, you need to handle the IOException, not merely log it, and you should close your I/O connections before you leave the subroutine.

Upvotes: 1

Related Questions