title: How to use Gson to serialize objects in android?
body: <p>I want to send 2 objects to Java server from Android client using socket (as I am developing a Remote PC). </p>
  
  <p><strong><code>AndroidClient.java</code></strong></p>
  
  <pre><code>    public class MainActivity extends Activity{
          Socket client;
          ObjectOutputStream oos;
          OutputStream os;
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
                      SendObj so=new SendObj();
                      so.execute();
          }
  
          class SendObj extends AsyncTask&lt;Void, Void, Void&gt;{
              @Override
              protected Void doInBackground(Void... arg0) {
                      try {
                          client=new Socket("192.168.237.1",6566);
                          os=client.getOutputStream();
                          oos=new ObjectOutputStream(os);
                          Serializer ma=new Serializer(2, "Helllo");
                          Log.i("Serial",""+ma.name);
                          oos.writeObject(ma);
                          oos.writeObject(new String("Another Object from Client"));
                          oos.close();
                          os.close();
                          client.close();
                      } catch (UnknownHostException e) {
                          e.printStackTrace();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                      return null;
              }
          }
  
          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
          }
  
      }
  
      @SuppressWarnings("serial")
      class Serializer  implements Serializable {
          int num;
          String name;
          public Serializer(int num,String name){
              this.name=name;
              this.num=num;
          }
      }
  </code></pre>
  
  <p><strong><code>JavaServer.java</code></strong></p>
  
  <pre><code>public class ObjectReceiver {
          static ServerSocket server;
          static Socket client;
          static ObjectInputStream ois;
          static InputStream is;
  
          public static void main(String[] arg) throws IOException, ClassNotFoundException{
              server=new ServerSocket(6566);
              System.out.println("Wait");
              while(true){
                  client=server.accept();
                  System.out.println("Connected");
                  is=client.getInputStream();
                  ois=new ObjectInputStream(is);
                  try {
                      ObjectSerail or = (ObjectSerail) ois.readObject();
                      if(or!=null){
                          System.out.println("Done");
                      }
                  } catch (ClassNotFoundException e) {
                      e.printStackTrace();
                  }
  
              }
          }
  
          @SuppressWarnings("serial")
          class ObjectSerail implements Serializable{
               int num;
               String name;
              public ObjectSerail(int num,String name){
                  this.name=name;
                  this.num=num;
              }
          }
      }
  </code></pre>
  
  <p>Of course I know the above method won't work because it will gives <code>ClassNotFoundException()</code>. So now I want to know how can I use Gson library to serialize and deserialize the objects. Thanks in advance.</p>