2011-05-01 64 views
2

我得到了此代碼格式Android-er blogspot,非常感謝他讓我幾乎瞭解了java中的基本套接字連接。所以我得到了我的android設備上的客戶端應用程序,以及運行服務器的計算機,但是我怎樣才能在客戶端代碼中進行循環,以便實時從EditText發送數據? (每當它改變時)如果有人可以清除它的一個完整的新手?Java套接字 - 實時傳輸

-----這是客戶端代碼(安卓-ER版權):

package com.exercise.AndroidClient; 

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.Socket; 
import java.net.UnknownHostException; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class AndroidClient extends Activity { 

EditText textOut; 
TextView textIn; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

textOut = (EditText)findViewById(R.id.textout); 
Button buttonSend = (Button)findViewById(R.id.send); 
textIn = (TextView)findViewById(R.id.textin); 
buttonSend.setOnClickListener(buttonSendOnClickListener); 
} 

Button.OnClickListener buttonSendOnClickListener 
= new Button.OnClickListener(){ 

@Override 
public void onClick(View arg0) { 
// TODO Auto-generated method stub 
Socket socket = null; 
DataOutputStream dataOutputStream = null; 
DataInputStream dataInputStream = null; 

try { 
    socket = new Socket("192.168.1.101", 8888); 
    dataOutputStream = new DataOutputStream(socket.getOutputStream()); 
    dataInputStream = new DataInputStream(socket.getInputStream()); 
    dataOutputStream.writeUTF(textOut.getText().toString()); 
    textIn.setText(dataInputStream.readUTF()); 
} catch (UnknownHostException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
finally{ 
    if (socket != null){ 
    try { 
    socket.close(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    } 

    if (dataOutputStream != null){ 
    try { 
    dataOutputStream.close(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    } 

    if (dataInputStream != null){ 
    try { 
    dataInputStream.close(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    } 
} 
}}; 
} 

-----這是服務器代碼(安卓-ER版權):

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.ServerSocket; 
import java.net.Socket; 


public class MyServer { 

public static void main(String[] args){ 
    ServerSocket serverSocket = null; 
    Socket socket = null; 
    DataInputStream dataInputStream = null; 
    DataOutputStream dataOutputStream = null; 

    try { 
    serverSocket = new ServerSocket(8888); 
    System.out.println("Listening :8888"); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 

    while(true){ 
    try { 
    socket = serverSocket.accept(); 
    dataInputStream = new DataInputStream(socket.getInputStream()); 
    dataOutputStream = new DataOutputStream(socket.getOutputStream()); 
    System.out.println("ip: " + socket.getInetAddress()); 
    System.out.println("message: " + dataInputStream.readUTF()); 
    dataOutputStream.writeUTF("Hello!"); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    finally{ 
    if(socket!= null){ 
    try { 
     socket.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 

    if(dataInputStream!= null){ 
    try { 
     dataInputStream.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 

    if(dataOutputStream!= null){ 
    try { 
     dataOutputStream.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 
    } 
    } 
} 
} 

回答

1

有幾件事你需要改變。

首先,如果你想在實時發送數據,則需要使用一個Button OnClickListener到使用TextWatcher(見addTextChangedListener在TextView

改變作爲本次活動將被解僱每次文本更改時,都需要在事件外打開套接字(每次輸入文本時都不需要新的套接字),然後在您的監聽器中,您只需要將新數據發送到插座。

+0

感謝您的幫助!我在事件onSensorChanged之外打開新的套接字 - 因爲我想發送到pc到傳感器數據,並且還創建新的dataOutputStream,但是當我在事件內部使用DataOutputStream.WriteUTF時,它只發送一次..那有什麼問題? – Boro 2011-05-01 16:31:49

+0

確保你沒有關閉你的套接字,並且確保你在每次寫入後刷新套接字。 – Codemwnci 2011-05-01 18:00:24

0

您可以在EditText上設置文本更改的偵聽器,然後從那裏發送。

edittext.addTextChangedListener(new TextWatcher() { 
    public void onTextChanged(CharSequence charSequence, int start, int before, int count) { 
// ... do your sending here 
} 

但要注意,如果發送不是異步的,它可能會阻止用戶的文本輸入。 GSM網絡上的網絡延遲可能相對較高,因此用戶可能會感到惱怒,因爲他剛輸入的字符不會立即出現在屏幕上。