0

我希望从我的 telnet 套接字输入中解析“艺术家 - 标题”。现在它只显示最后一行是“END”。我做错了什么?我是 java.pls 的初学者,请帮助...

我的工作脚本:

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();
    }

}

我的 System.out.println http://pastebin.com/SkfiPXar

4

2 回答 2

1

readLine()您在打印前执行额外的操作。折叠这三行:

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

进入

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

此外,看起来您关闭了Socket内部while循环,这可能会导致问题。

于 2012-10-30T14:23:29.930 回答
0

完整活动:

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

于 2012-10-30T16:14:46.147 回答