![]() |
|
Programación y Desarrollo para Android Subforo exclusivo para temas de programación de software para PDAs y desarrollo de aplicaciones, interfaces, etc bajo Android |
«
Tema Anterior
|
Siguiente tema
»
|
Herramientas |
#1
|
||||
|
||||
Cliente/Servidor
Buenas! tengo un problema y es que me he creado un cliente/servidor muy simple. Lo que haria la app es pedir una cadena de caracteres por pantalla y al pulsar un boton enviarla al servidor pero me falla la conexion con el servidor. El mensaje que me aparece es el siguiente:
No command output when running: 'am start -n com.example.serverpillao/com.example.serverpillao.MainActivity -a android.intent.action.MAIN -c android.intent.category.LAUNCHER' on device emulator-5554 Este seria el MainActivity: package com.example.serverpillao; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener{ public static final String SERVERIP = "192.168.1.255"; // 'Within' the emulator! public static final int SERVERPORT = 80; public TextView text1; public EditText input; public Button btn; public boolean start; public Handler Handler; /** Called when the activity is first created. * @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text1=(TextView)findViewById(R.id.textView1); input=(EditText)findViewById(R.id.editText1); btn = (Button)findViewById(R.id.button1); btn.setOnClickListener(this); start=false; new Thread(new Server()).start(); try { Thread.sleep(500); } catch (InterruptedException e) { } new Thread(new Client()).start(); Handler = new Handler() { @Override public void handleMessage(Message msg) { String text = (String)msg.obj; text1.append(text); } }; } @SuppressLint("NewApi") public class Client implements Runnable { @Override public void run() { while(start==false) { } try { Thread.sleep(500); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { InetAddress serverAddr = InetAddress.getByName(SERVERIP); updatetrack("Client: Start connecting\n"); DatagramSocket socket = new DatagramSocket(); byte[] buf; if(!input.getText().toString().isEmpty()) { buf=input.getText().toString().getBytes(); } else { buf = ("Default message").getBytes(); } DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, SERVERPORT); updatetrack("Client: Sending '" + new String(buf) + "'\n"); socket.send(packet); updatetrack("Client: Message sent\n"); updatetrack("Client: Succeed!\n"); } catch (Exception e) { updatetrack("Client: Error!\n"); } } } public class Server implements Runnable { @Override public void run() { while(start==false) { } try { InetAddress serverAddr = InetAddress.getByName(SERVERIP); updatetrack("\nServer: Start connecting\n"); DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr); byte[] buf = new byte[17]; DatagramPacket packet = new DatagramPacket(buf, buf.length); updatetrack("Server: Receiving\n"); socket.receive(packet); updatetrack("Server: Message received: '" + new String(packet.getData()) + "'\n"); updatetrack("Server: Succeed!\n"); } catch (Exception e) { updatetrack("Server: Error!\n"); } } } @Override public void onClick(View v) { // TODO Auto-generated method stub start=true; } public void updatetrack(String s){ Message msg = new Message(); String textTochange = s; msg.obj = textTochange; Handler.sendMessage(msg); } } Y este el activity_main: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android ![]() <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Communication History:" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> Al ejecutarlo me aparece el mensaje de Error! es decir, que entra en el catch (Exception e) ¿Alguien tiene idea de a qué se puede deber esto? Muchas gracias de antemano. Saludos. |
|
#2
|
||||
|
||||
No puedes probarlo en un móvil?
|
#3
|
||||
|
||||
Si, si puedo probarlo pero ya te digo, escribo el mensaje, lo envío, saca el mensaje de que el cliente lo manda correctamente pero justo después aparece el mensaje de Error! de haber entrado en el catch (Exception e). La verdad es que ya no sé que hacer ni qué probar así que acepto todo tipo de ideas
![]() |
#4
|
Prueba a usar peticiones REST para el servidor y manejar respuestas en JSON:
Te dejo aqui un tutorial: http://www.sgoliver.net/blog/?page_id=3011 |
Respuesta |
![]() |
||||||
|
«
Tema Anterior
|
Siguiente tema
»
Herramientas | |
|
|
Hora actual: 00:05:49 (GMT +2)
HTCMania: líderes desde el 2007