簡體   English   中英

Android套接字錯誤或崩潰

[英]Android Socket Error or crash

我正在使用以下代碼將數據發送到TCP服務器,但出現錯誤。 任何想法? 我需要使用線程嗎? 我可以直接在函數上運行它嗎?

Msg&IPo&Proto是布局中的“編輯框”。 我認為我的問題是使用套接字!

    Socket Clientsocketo;
String MsgOut="Nothing Happend";
TextView ad;

public void SendData(View v) throws IOException {
    EditText aa = (EditText)findViewById(R.id.EditText01);
    String Msg = aa.getText().toString();

    EditText ab = (EditText)findViewById(R.id.editText);
    String IPo = ab.getText().toString();

    EditText ac = (EditText)findViewById(R.id.editText2);
     int Porto  = Integer.parseInt(ac.getText().toString());

     InetAddress serverAddress = InetAddress.getByName(IPo);
    try{
        Clientsocketo = new Socket(serverAddress,Porto);
        ObjectOutputStream oos = new ObjectOutputStream(Clientsocketo.getOutputStream());
        oos.writeObject("Hello World");

        ObjectInputStream ois = new ObjectInputStream(Clientsocketo.getInputStream());
        String massagedf = (String)ois.readObject();
        ad.setText(massagedf);
        // mHandler.sendMessage(serverMessage);

        oos.close();
        ois.close();
    }
    catch(Exception e){
        ad.setText("Error");
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ad = (TextView) findViewById(R.id.textView);
}

網絡操作必須在AsyncTask中處理。 這允許網絡操作繼續進行而不會阻塞主線程。 使用AsyncTask的最好方法是創建一個子類。 像這樣:

public class SendDataTask extends AsyncTask<Void, Void, Void> {

    Socket clientSocketTo;

    String msg;
    String ipo;
    int porto;
    InetAddress serverAddress;

    public SendDataTask(View v){
        EditText aa = (EditText) v.findViewById(R.id.EditText01);
        msg = aa.getText().toString

        EditText ab = (EditText) v.findViewById(R.id.editText);
        ipo = ab.getText().toString();

        EditText ac = (EditText) v.findViewById(R.id.editText2);
        porto = Integer.parseInt(ac.getText().toString());
        serverAddress = InetAddress.getByName(ipo);

    }

    protected void doInBackground(Void... arg){
        try{
            clientSocketTo = new Socket(ServerAddress,Porto);
            ObjectOutputStream oos = new ObjectOutputStream(clientSocketTo.getOutputStream());
            oos.weriteObject("Hello World")

            ObjectInputStream ois = new ObjectInputStream(clientSocketTo.getInputStream());
            String massagedf = (String) ois.readObject();
            ad.setText(massagedf);

            oos.close();
            ois.close();

        }catch (Exception e){

        }

    }

    protected void onPostExecute(Void result){

    }

}

要調用此方法,您將實例化傳遞您的視圖的類,然后調用execute方法:

new SendDataTask(yourView).execute();

此代碼均未經過測試...希望對您有所幫助!

更多信息: http : //developer.android.com/reference/android/os/AsyncTask.html http://androidsrc.net/android-client-server-using-sockets-client-implementation/

暫無
暫無

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

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