Android Network Programming?


Last Updated:

  1. mrboombastic

    mrboombastic Member This Topic's Starter

    Joined:
    Aug 7, 2010
    Messages:
    5
    Likes Received:
    0
    Does anyone know the right way to write a networking program for Android?

    I attempted to send an object from my phone to a regular socketServer. It uses a ObjectOutputStream & the Socket.accept() method.

    The android client seems to be working. It seems to send the object.
    However, the server doesnt seem to accept the object.

    Does anyone know if its possible to create a networking program in this way for Android?

    Here's the client
    Code (Text):
    1.  
    2. package com.example.Plus;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.widget.TextView;
    8. import android.widget.Toast;
    9. import android.widget.EditText;
    10. import java.net.*;
    11. import java.io.*;
    12.  
    13.  
    14. public class PlusRemote extends Activity {
    15.    
    16.     private EditText text;
    17.     //InetAddress address;
    18.     Socket s;
    19.     OutputStream out=null;
    20.     ObjectOutputStream oos=null;
    21.     String cmd ="";
    22.    
    23.     /** Called when the activity is first created. */
    24.     @Override
    25.     public void onCreate(Bundle savedInstanceState) {
    26.         super.onCreate(savedInstanceState);
    27.         setContentView(R.layout.main);
    28.         text = (EditText) findViewById(R.id.defaultIp);
    29.     }
    30.     public void myClickHandler(View view) {        
    31.         switch (view.getId()){
    32.         case R.id.CON:
    33.             cmd = "Connect";
    34.             //Connect to Server
    35.             try{
    36.                 this.text.setText("Connecting....");
    37.            
    38.                 //substitute x's for your real ip address
    39.                 s=new Socket("xxx.xxx.xxx.xxx", 1234);
    40.                 out=s.getOutputStream();
    41.                 oos=new ObjectOutputStream(out);
    42.                 oos.writeObject(new String(cmd));
    43.                 oos.flush();
    44.                 oos.close();
    45.                 s.close();
    46.             }catch(Exception e){
    47.                 this.text.setText(e.toString());
    48.                     }
    49.             //SET status "Connected Success!"
    50.             this.text.setText(R.string.EditText01);
    51.            
    52.             break;
    53.         case R.id.REW:
    54.     Toast.makeText(this, "REWIND ", Toast.LENGTH_LONG).show();
    55.             break;
    56.         case R.id.STOP:
    57.             Toast.makeText(this, "STOP", Toast.LENGTH_LONG).show();
    58.             break;
    59.         case R.id.PLAY:
    60.             Toast.makeText(this, "PLAY", Toast.LENGTH_LONG).show();
    61.             break;
    62.         case R.id.FWD:
    63.             Toast.makeText(this, "FOWARD", Toast.LENGTH_LONG).show();
    64.             break;
    65.         }
    66.      }
    67.  
    68.  
    69. }
    70.  
    And heres the server
    Code (Text):
    1.  
    2. //PlusServer.java
    3. //Server that takes commands from Android Client, executes commands & finally returns result.
    4.  
    5. import java.net.*;
    6. import java.io.*;
    7.  
    8. public class PlusServer
    9. {
    10.     //instance variables
    11.     static Boolean finished = false;
    12.  
    13.    
    14.     //Main method
    15.     public static void main(String[] args)
    16.         {
    17.         System.out.println(" 000 Entered Main method");
    18.         //Disable Security Manager
    19.         //System.setSecurityManager(null);
    20.             try
    21.             {
    22.         ServerSocket listener = new ServerSocket(1234);
    23.         System.out.println("0 setup ServerSocket on port 1234");
    24.  
    25.             while(!finished)
    26.                 {
    27.          
    28.                 Socket client = listener.accept();//wait for connection
    29.        
    30.                 InputStream in = client.getInputStream();
    31.                 OutputStream out = client.getOutputStream();
    32.              
    33.                 //read cmd
    34.                 ObjectInputStream oin = new ObjectInputStream(in);
    35.  
    36.                 String cmd = (String)oin.readObject();
    37.  
    38.                 if (cmd.equals("Connect"))
    39.                     {
    40.        
    41.                     client.close();
    42.                     finished=true;
    43.                     }
    44.                 //Send results
    45.                 // blah, blah, blah
    46.  
    47.                    
    48.                
    49.                 }
    50.            
    51.             listener.close();    
    52.             }
    53.             catch(Exception e){System.out.println("Error: "+e);}
    54.         }
    55. }
    56.  
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    I just did something very similar using bluetooth instead of HTTP, but instead of using ObjectInputStream, I used a regular ol' InputStream and used a regular ol' OutputStream on the other end. I have also got it working using BufferedInputStream/BufferedOutputStream, but when just sending a simple string of text, the differences were negligible, so I was able to cut out a lot of code by just using a regular Input/OutputStream. Might try that as I really don't see a need to read in an object.
     

Share This Page

Loading...