簡體   English   中英

如何正確使用InputStream.read(buffer)方法並處理我的數據?

[英]How to use correctly InputStream.read(buffer) method and process my Data?

我必須在Android中創建一個應用程序,以將我的Galaxy S3與另一台設備連接,在此答案中,我將其稱為device1。 我使用套接字和InputStream。 -首先:我使用插座連接手機,建立連接后,device1向我發送了一些存儲在一個緩沖區中的字符串。

  • 第二:我向device1發送一個“ s”字符串,當device1收到該字符串時,它將連續不斷地向我發送數據。

-tree:我想讀取這些數據並進行詳細說明

問題在於InputStream.read(buffer)正在阻塞,因此我接收到數據,但無法處理它。

這是我的代碼:

public void run() {             

                try {
                    socket=new Socket();  
                    socket.connect(new InetSocketAddress(ipDevice,portDevice),20); //settare il timeout per evitare che tutto si blocchi su receive
                    String controllo="connesso";





                    showToast(controllo);
                    InputStream is = socket.getInputStream();
                    byte[] buffer_controllo = new byte[1024];
                                            int count_bytes_read;
                    char[] chars ;
                    String str;

                    count_bytes_read = is.read(buffer_controllo); //lettura del buffer

                    str=new String(buffer_controllo);
                    chars=new char[count_bytes_read];



                    OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
                    BufferedWriter bw = new BufferedWriter(osw);
                    PrintWriter out = new PrintWriter(bw, true);
                    out.write("s");
                    byte[] buffer_dati = new byte[1024];
                    int count_bytes_read2=0;
                    while(count_bytes_read2!=-1){
                    count_bytes_read2 = is.read(buffer_dati); //PROBLEM HERE
                    }
                    str=new String(buffer_dati);
                    chars=new char[count_bytes_read];

                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    String controllo="non connesso";
                    showToast(controllo);
                    try {
                        socket.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    e.printStackTrace();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    String controllo="non connesso";
                    showToast(controllo);
                    try {
                        socket.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    e.printStackTrace();                    
                }  
                finally{
                    if(socket!=null){
                        try {
                            OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
                            BufferedWriter bw = new BufferedWriter(osw);
                            PrintWriter out = new PrintWriter(bw, true);
                            out.write("z");                 
                            out.flush();
                            socket.close();                         
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }

        });
        //Avvio del Thread
        cThread.start();

如您所見,問題出在這里:

count_bytes_read2 = is.read(buffer_dati);

此讀取方法被阻止,我從device1接收了無數數據,因此我無法取出緩沖區dati並對其進行詳細說明。 在執行read方法期間如何獲取這些數據? 我必須阻止閱讀一段時間或還有其他解決方案? 謝謝。

DataFetcher是我編寫的用於處理此類問題的類。 它是一個線程化的閱讀器,它通過偵聽器/回調模式進行操作……盡管它也可以用於完全讀取流。 我認為這可能對解決您的問題很有用

您的代碼沒有意義。 您從流中讀取數據直到流結束,然后才對數據進行任何處理。

數據處理應在讀取循環內進行。

當然,如果設備無限發送,它將永遠不會斷開連接,因此流的末端將永遠不會到達。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM