2013-07-12 36 views
1

我正在研究一個UDP程序,但我在應用setText在我的asynctask中遇到問題。基本上在UDP服務器上,我只是要求輸入一個端口,然後服務器應該連接到本地主機和端口。位於佈局中間的TextView顯示「目前未連接」,當點擊連接到端口時,我希望它更改爲「已連接」。但是,它沒有做任何事情。我試着四處搜尋,但無法提出解決方案。如果任何人都可以幫助我找出這個問題,將不勝感激。在asynctask中的Android setText

我的線程asynctask代碼: 編輯:用setText outin doinbackground。但是,setText仍然不起作用。

public class ServerThread extends AsyncTask<String, String, String>{ 
    TextView chatbox; 
    TextView amiconnected; 

    @Override 
    protected void onPreExecute(){ 
     chatbox = (TextView) findViewById(R.id.textView2); 
     amiconnected = (TextView) findViewById(R.id.textView3); 
     PORT = Integer.parseInt(((EditText) findViewById(R.id.editText1)).getText().toString()); 
    } 
    @Override 
    protected String doInBackground(String... arg0) { 

     //open socket at specified port on the local host 
     try { 
      socket = new DatagramSocket(PORT); 
      //listening 

      byte[] buf = new byte[1024]; 
      DatagramPacket packet = new DatagramPacket(buf, buf.length); 
      socket.receive(packet); 
      text2 = new String("Now you're connected."); 
      //convert received message to string 
      text1 = new String(buf, 0, packet.getLength()); 

      //get client address from received packet 
      //client_addr = packet.getAddress(); 
      //byte[] message = new byte[1024]; 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return text2; 
    } 

    @Override 
    protected void onPostExecute(String text2) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(text2); 
     chatbox.setText(text2); 

    } 

編輯:我的整個UDP服務器代碼

package com.example.udpcomm; 

import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 

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

public class Server extends Activity{ 
    int PORT; 
    DatagramSocket socket; 
    InetAddress client_addr; 
    String text1; 
    String text2; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_server); 
     //chatbox = (TextView) findViewById(R.id.textView2); 
     Button connectButton = (Button) findViewById(R.id.connectserver); 
     connectButton.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick(View v){ 
       new ServerThread().execute(); 
      } 
     }); 
    } 
    //parameter string port (converted into int) 
    //onprogressupdate (inputting string (?) into TextView) 
    //on postexecute return null (?) 
    public class ServerThread extends AsyncTask<String, String, String>{ 
     TextView chatbox; 
     TextView amiconnected; 

     @Override 
     protected void onPreExecute(){ 
      chatbox = (TextView) findViewById(R.id.textView2); 
      amiconnected = (TextView) findViewById(R.id.textView3); 
      PORT = Integer.parseInt(((EditText) findViewById(R.id.editText1)).getText().toString()); 
     } 
     @Override 
     protected String doInBackground(String... arg0) { 

      //open socket at specified port on the local host 
      try { 
       socket = new DatagramSocket(PORT); 
       //listening 

       byte[] buf = new byte[1024]; 
       DatagramPacket packet = new DatagramPacket(buf, buf.length); 
       socket.receive(packet); 
       text2 = new String("Now you're connected."); 
       //convert received message to string 
       text1 = new String(buf, 0, packet.getLength()); 

       //get client address from received packet 
       //client_addr = packet.getAddress(); 
       //byte[] message = new byte[1024]; 

      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return text2; 
     } 

     @Override 
     protected void onPostExecute(String text2) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(text2); 
      chatbox.setText(text2); 

     } 

    } 


} 
+0

您需要閱讀文檔。 AsyncTask的一半是管理UI線程和工作線程之間的交互。不要在'doInBackground'中執行'setText'。您可以根據需要在'onProgressUpdate'或者'onPostExecute'中執行。 – kabuko

回答

7

您需要將您的UI行動的onPreExecute()onPostExecute()方法爲doInBackground不能碰UI。我建議你將返回的字符串或值從doInBackground方法中放入TextView,然後將其應用於onPostExecute()方法中的TextView。參考AsyncTask Description

+0

感謝您的輸入。我試着按照你的建議進行修改,因爲它似乎是我已經擁有的最直接的方式。但是,我仍然沒有在「連接」TextView中獲得更改,這是text2值。你是否碰巧在我的代碼中看到其他問題? (儘管我有text1的定義,爲了簡單起見我沒有使用atm)。 – user2562568

+0

你現在可以發佈你的當前代碼,讓我看看我是否可以識別其他:) – kabuto178

+0

我剛剛發佈了我的整個UDP服務器代碼(因爲它不只是線程是問題所在)。感謝您看看它 – user2562568