2013-07-12 40 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