解析套接字输入字符串

时间:2012-10-30 14:18:44

标签: java android

我希望从我的telnet套接字输入中解析“artist - title”。现在它只显示最后一行“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

2 个答案:

答案 0 :(得分:1)

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

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

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

此外,您似乎在Socket循环内关闭了while,这可能会导致问题。

答案 1 :(得分: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