Question

I am having a bit of an issue with my app. I receive a data through a socket, via a BufferedReader. I loop round with while ((sLine = reader.readLine ()) != null) and append the sLine to a StringBuilder object. I also spend a new line \n to the builder.

The plan is that once the builder is all finished, String sTotal = builder.toString()is called and a total is passed to the next routine.

However, the next routine is instead being called once for each line rather than with the string as a whole. The routine call is outside the loop above so I really don't know why!

Hope someone can help...

Edit: Code extract below.

public void run() {
    try {
        oServerSocket = new ServerSocket(iPort);

        while ((!Thread.currentThread().isInterrupted()) && (!bStopThread)) {
            try {
                oSocket = oServerSocket.accept();
                this.brInput = new BufferedReader(new InputStreamReader(this.oSocket.getInputStream()));
                StringBuilder sbReadTotal = new StringBuilder();
                String sReadXML = "";
                while ((sReadXML = brInput.readLine()) != null) {
                    sbReadTotal.append("\n");
                    sbReadTotal.append(sReadXML);
                }
                sReadXML = sbReadTotal.toString();
                Log.d("XMLDATA", sReadXML);
                processXML(sReadXML);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    } catch (Exception e) {
        /* Nothing Yet */
        e.printStackTrace();
    }
}
Was it helpful?

Solution

If you're exiting your internal while loop, it means you reached the end of your input stream (that's when readLine() returns null according to the docs).

You should be looking into the client, and not the server. What's establishing the client socket? Are you sure it's not establishing a separate connection for each line it sends?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top