2014-02-08 25 views
0
package com.asyncu; 

import android.os.Bundle; 
import android.app.Activity; 
//import android.app.Notification; 
import android.util.Log; 
import android.view.*; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.*; 

import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 

import android.annotation.SuppressLint; 
import android.os.AsyncTask; 
import android.os.Build; 

import java.net.InetAddress; 
import java.net.SocketException; 
import java.net.UnknownHostException; 


public class MainActivity extends Activity { 
    Button bttn; 
    CheckedTextView ctv; 
    TextView result; 
    Button rset; 
    NetworkTask networktask; 
    byte[] buffer = new byte[256]; 
    Boolean connected=false; 
    DatagramSocket ds=null; 
    boolean x=true; 
    static int packetCount = 0; 
    byte[] p; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     bttn=(Button)findViewById(R.id.Button1); 
     result=(TextView)findViewById(R.id.result); 
     bttn.setOnClickListener(buttonConnectOnClickListener); 
    } 
    private OnClickListener buttonConnectOnClickListener = new OnClickListener() { 
     public void onClick(View v){ 
      if(!connected){//if not connected 
       System.out.println("connecting to Server"); 
       networktask = new NetworkTask(); //New instance of NetworkTask 
       networktask.execute(); 
      } 
      else{ 
       System.out.println("disconnecting from Server..."); 
       if(networktask!=null){ 
        networktask.closeSocket(); 
        networktask.cancel(true); 
       } 

      } 
     } 
    }; 
    public class NetworkTask extends AsyncTask<String, String, String> 
    { 

     @Override 
     protected String doInBackground(String... params) 
     { 
      //boolean res= false; 

      try 
      { 
       Log.i("AsyncTask", "doInBackground: Creating socket"); 
       ds=new DatagramSocket(5000); 
       Log.i("AsyncTask", "doInBackground: socket created"); 
       DatagramPacket recv_packet = new DatagramPacket(buffer, buffer.length); 
       packetCount = 0; 
       while(x) 
        { 
        System.out.println("in while"); 

        Log.i("AsyncTask", "doInBackground: in while"); 

        ds.receive(recv_packet); 
        Log.i("AsyncTask", "doInBackground: packets received"); 
        packetCount++; 

        publishProgress(""+packetCount); 
        Log.i("AsyncTask", "doInBackground: after publish"); 



        } 
      } 
      catch(RuntimeException e) 
      { 
       System.out.println("Error"+e.getMessage()+"\n"+e.getStackTrace().toString()); 
      } 
      catch(Exception e) 
      { 
       System.out.println("Error"+e.getMessage()+"\n"+e.getStackTrace().toString()); 
      } 
     finally 
     { 
      closeSocket(); 
     } 
      return null; 
    } 
     void closeSocket() 
     { 
      ds.close(); 
      x=false; 
     } 
     public void onProgressUpdate(String... currentPacketCount) 
     { 
      super.onProgressUpdate(currentPacketCount); 
      Log.e("AsyncTask", "doInBackground: " + currentPacketCount.toString()); 
      result.setText(""+currentPacketCount); 
     } 

    } 


} 

我想在android中開發一個UDP客戶端,它接收來自服務器的數據包並遞增計數器,同時將值打印到UI。 logcat顯示接收到數據包。但計數器值不顯示。我無法追蹤我出錯的地方。 請檢查我的代碼並提供有用的見解。先謝謝你。無法更新我的Android應用程序中的用戶界面

佈局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <ToggleButton 
     android:id="@+id/Button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/checkedTextView1" 
     android:layout_below="@+id/checkedTextView1" 
     android:text="ToggleButton" 
     android:textOff="Connect" 
     android:textOn="Disconnect" /> 


    <CheckedTextView 
     android:id="@+id/checkedTextView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/checkedTextView1" 
     android:text="No. of packets received" /> 

    <TextView 
     android:id="@+id/result" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/Button1" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="42dp" 
     android:text="result" /> 

</RelativeLayout> 
+0

看起來好了給我一個代碼前,也許問題在於你的activity_main.xml佈局文件? –

回答

1

刪除此行

result.setText(""+currentPacketCount); 

加入這一行

result.setText(""+currentPacketCount[0]); 
相關問題