Reputation: 43
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);
}
}
}
I am trying to develop a UDP client in android which receives packets from server and increments counter simultaneously printing the value to UI. logcat shows that packets are received. But the counter value is not displayed. I am unable to track where i went wrong. Please check my code and give your helpful insight. Thank you in advance.
layout file
<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>
Upvotes: 0
Views: 73
Reputation: 1069
remove this line
result.setText(""+currentPacketCount);
add this line
result.setText(""+currentPacketCount[0]);
Upvotes: 1