0

i wish to parse "artist - title" from my telnet socket input. Right now It's only display a last line which is "END". what i'm doing wrong? im beginner with java.pls help...

My activity script which to the work:

try {
        Socket s = new Socket("10.0.2.2", 5555);


        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), "ISO8859_1"));



        String on_air = "request.on_air" + System.getProperty("line.separator");
        out.write(on_air);
        out.flush();
        Log.i("TcpClient", "sent: " + on_air);
        String on_airin = in.readLine() + System.getProperty("line.separator");
        Log.i("TcpClient", "received: " + on_airin);
        String metadata = "ao.metadata "+ on_airin + System.getProperty("line.separator"); 
        out.write(metadata);
        out.flush();
        Log.i("TcpClient", "sent2: " + metadata);
        String metadatain = in.readLine() + System.getProperty("line.separator");
        Log.i("TcpClient", "received: " + metadatain);

        String line = in.readLine();
        while (line != null) {

            line = in.readLine();


            System.out.println(line);
            output = (TextView) findViewById(R.id.textView2);
            output.setText(line);





//tv.setText(online + formatedDate );
        output = (TextView) findViewById(R.id.textView1);



s.close();

    }} catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

My System.out.println http://pastebin.com/SkfiPXar

2
  • 1
    Correct your while condition here : while (line != null) Commented Oct 30, 2012 at 14:20
  • 1
    you are closing the socket inside the loop ... Commented Oct 30, 2012 at 14:40

2 Answers 2

1

You perform an extra readLine() before printing. Collapse these three lines:

String line = in.readLine();
while (line != null) {
    line = in.readLine();

into

String line = null;
while ((line = in.readLine()) != null) {  

Also, it looks like you close your Socket inside the while loop, which might cause problems.

Sign up to request clarification or add additional context in comments.

1 Comment

tnx, i fix the code a little bit. But i have no idea how to parse right variables like artist or title from String metadatain?
0

Full Activity:

public class MainActivity extends Activity {


//TextView output; //textview to display the artist
TextView artist;
//TextView output; //textview to display the duration
TextView time;
/** Called when the activity is first created. */



public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);






    try {
        Socket s = new Socket("10.0.2.2", 5555);


        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), "ISO8859_1"));

        //get audio estimated remaining time
        String milliseconds = "ao.remaining" + System.getProperty("line.separator");
        out.write(milliseconds);
        out.flush();
        Log.i("TcpClient", "sent: " + milliseconds);
        String milliseconds_in = in.readLine() + System.getProperty("line.separator");
        Log.i("TcpClient", "received: " + milliseconds_in);



        //get metadata stuff  
        String on_air = "request.on_air" + System.getProperty("line.separator");
        out.write(on_air);
        out.flush();
        Log.i("TcpClient", "sent: " + on_air);
        String on_airin = in.readLine() + System.getProperty("line.separator");
        Log.i("TcpClient", "received: " + on_airin);
        String metadata = "ao.metadata "+ on_airin + System.getProperty("line.separator"); 
        out.write(metadata);
        out.flush();
        Log.i("TcpClient", "sent: " + metadata);
        String metadatain = in.readLine() + System.getProperty("line.separator");
        Log.i("TcpClient", "received: " + metadatain);






        String closetelnet = "exit" + System.getProperty("line.separator"); 
        out.write(closetelnet);
        out.flush();
        Log.i("TcpClient", "sent: " + closetelnet);
        String closetelnetin = in.readLine() + System.getProperty("line.separator");
        Log.i("TcpClient", "received: " + closetelnetin);


        String line = null;
        while ((line = in.readLine()) != null) {


            System.out.println(line);
            artist = (TextView) findViewById(R.id.textView2);
            artist.setText(line);

            float seconds = Float.parseFloat(milliseconds_in);
            Date date = new Date((long)(seconds * 1000));
            SimpleDateFormat formater = new SimpleDateFormat("mm:ss");
            String formatedDate = formater.format(date);
            //android.util.Log.d("This will display:", formatedDate);

            time = (TextView) findViewById(R.id.textView1);
            time.setText(formatedDate);


    }   
        s.close();

    }catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

System.out http://pastebin.com/G1P1HrvA

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.