Note that there are some explanatory texts on larger screens.

plurals
  1. POSocket can be send only once and the Textview is not gathering the information from the server
    primarykey
    data
    text
    <p>I had two issues in the program I am making... the thing is that I want to send by a Edittext that information to a server via UDP....the thing is that the program only works the first time I run the program, I mean, if i open the application and write some text, the information is sended to the server, but if I type another thing and press to button so the new information is sended it doesn't work... the other thing is that the TextView is not appending the information, and the server send some info but the application is not gathering that information... so if someone had a clue why is this happening or what I am doing wrong I appreciated any help!... Thanks in advice...</p> <p>here is the code:</p> <p><code></p> <p>public class MainActivity extends Activity implements View.OnClickListener {</p> <pre><code>public static final String SERVERIP = "190.99.20.200"; public static final int SERVERPORT = 5153; public TextView serverResponse; public EditText messageToSend; public Button btnSend; public boolean start; public Handler handler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); serverResponse = (TextView)findViewById(R.id.textView); messageToSend = (EditText)findViewById(R.id.editText); btnSend = (Button)findViewById(R.id.button); btnSend.setOnClickListener(this); start = false; new Thread(new Server()).start(); try{ Thread.sleep(500); }catch (InterruptedException e){ updatetrack("Error on Server:" + e.getMessage()); } new Thread(new Client()).start(); handler = new Handler(){ public void handledMessage(Message msg){ String text = (String)msg.obj; serverResponse.append(text); } }; } public class Client implements Runnable { @Override public void run() { while(start == false) { } try{ Thread.sleep(500); }catch (InterruptedException e1){ e1.printStackTrace(); } try{ InetAddress serverAddres = InetAddress.getByName(SERVERIP); updatetrack("Client:Start connectingn"); DatagramSocket socket = new DatagramSocket(); byte[] buffer; if(!messageToSend.getText().toString().isEmpty()) { buffer = messageToSend.getText().toString().getBytes(); } else { buffer = ("Message from android").getBytes(); } DatagramPacket packet = new DatagramPacket(buffer, buffer.length,serverAddres,SERVERPORT); updatetrack("Client:Sending" + new String(buffer)+ "'n"); socket.send(packet); updatetrack("Client: Messange sentn"); updatetrack("Client: Succed!n "); socket.close(); }catch (Exception e){ updatetrack("Client:Error!n" + e.getMessage()); } } } public class Server implements Runnable{ @Override public void run() { while (start == false) { } try{ InetAddress serverAddress = InetAddress.getByName(SERVERIP); updatetrack("nServer: Start connectingn"); DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddress); byte[] buffer = new byte[17]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); updatetrack("Server: Receivingn"); socket.receive(packet); updatetrack("Server: Message received:" + new String(packet.getData())+"'n"); updatetrack("Server : Succed!n"); }catch (Exception e){ updatetrack("Server: Error!n"+ e.getMessage()); } } } public void onClick(View view) { start = true; } public void updatetrack(String s) { Message msg = new Message(); String textTochange = s; msg.obj = textTochange; handler.sendMessage(msg); } </code></pre> <p>}</p> <p></code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload