Note that there are some explanatory texts on larger screens.

plurals
  1. POSend objects between Client and server android
    text
    copied!<p>i am trying to make an client server app . I have made the hole connection and it works fine. My server runs t my PC and my Client to my android mobile phone.I want to send and receive objects from client to server , i have made the server send objects but i do not know how to receive them in my client. My object class is PacketManager.</p> <p>public class PacketManager implements Serializable {</p> <pre><code>public String choise; public int amount; //Constructor PacketManager(String ep,int p){ choise=ep; amount=p; } </code></pre> <p>}</p> <p>And my MainActivity :</p> <p>public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);</p> <pre><code> arrayList = new ArrayList&lt;String&gt;(); mList = (ListView)findViewById(R.id.list); mAdapter = new MyCustomAdapter(this, arrayList); mList.setAdapter(mAdapter); // connect to the server new connectTask().execute(""); } public class connectTask extends AsyncTask&lt;String,String,TCPClient&gt; { @Override protected TCPClient doInBackground(String... message) { //we create a TCPClient object and mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() { @Override //here the messageReceived method is implemented public void messageReceived(String message) { //this method calls the onProgressUpdate publishProgress(message); } }); mTcpClient.run(); return null; } @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); //in the arrayList we add the messaged received from server arrayList.add(values[0]); // notify the adapter that the data set has changed. This means that new message received // from server was added to the list mAdapter.notifyDataSetChanged(); } } } </code></pre> <p>Finally my client class : </p> <p>PrintWriter out; BufferedReader in;</p> <pre><code>public TCPClient(OnMessageReceived listener) { mMessageListener = listener; } /** * Sends the message entered by client to the server * @param message text entered by client */ public void sendMessage(String message){ if (out != null &amp;&amp; !out.checkError()) { out.println(message); out.flush(); } } public void stopClient(){ mRun = false; } public void run() { mRun = true; try { //here you must put your computer's IP address. InetAddress serverAddr = InetAddress.getByName(SERVERIP); Log.e("TCP Client", "C: Connecting..."); //create a socket to make the connection with the server Socket socket = new Socket(serverAddr, SERVERPORT); try { //send the message to the server out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); Log.e("TCP Client", "C: Sent."); Log.e("TCP Client", "C: Done."); //receive the message which the server sends back in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //in this while the client listens for the messages sent by the server while (mRun) { serverMessage = in.readLine(); if (serverMessage != null &amp;&amp; mMessageListener != null) { //call the method messageReceived from MyActivity class mMessageListener.messageReceived(serverMessage); } serverMessage = null; } Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'"); } catch (Exception e) { Log.e("TCP", "S: Error", e); } finally { //the socket must be closed. It is not possible to reconnect to this socket // after it is closed, which means a new socket instance has to be created. socket.close(); } } catch (Exception e) { Log.e("TCP", "C: Error", e); } } //Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity //class at on asynckTask doInBackground public interface OnMessageReceived { public void messageReceived(String message); } </code></pre> <p>}</p> <p>Can Anyone help me with this problem , alla the connection works fine but i want to receive the Pack object from the server but i do not know the way. I have try to change the type of public void messageReceived(String message) to public void messageReceived(PackageManager Pack) but then i have proble with protected TCPClient doInBackground(String... message) and protected void onProgressUpdate(String... values). </p> <p>Server code</p> <pre><code>enter code here public TCPServer(OnMessageReceived messageListener) { this.messageListener = messageListener; public void sendMessage(String message) throws IOException{ if (mOut != null &amp;&amp; !mOut.checkError()) { mOut.println(message); mOut.flush(); } }@Override public void run() { super.run(); running = true;try {System.out.println("S: Connecting..."); //create a server socket. A server socket waits for requests to come in over the network. ServerSocket serverSocket = new ServerSocket(SERVERPORT); //create client socket... the method accept() listens for a connection to be made to this socket and accepts it. Socket client = serverSocket.accept(); System.out.println("S: Receiving..."); try {//sends the message to the client mOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true); //read the message received from client BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); //in this while we wait to receive messages from client (it's an infinite loop) //this while it's like a listener for messages while (running) { String message = in.readLine(); if (message != null &amp;&amp; messageListener != null) { //call the method messageReceived from ServerBoard class messageListener.messageReceived(message); } }catch (Exception e) { System.out.println("S: Error"); e.printStackTrace(); } finally { client.close(); System.out.println("S: Done."); } } catch (Exception e) { System.out.println("S: Error"); e.printStackTrace(); } } //Declare the interface. The method messageReceived(String message) will must be implemented in the ServerBoard //class at on startServer button click public interface OnMessageReceived { public void messageReceived(String message); } </code></pre>
 

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