Object serialization

Put your problem here if it does not fit any of the other categories.

Postby burakkilic » Sun Oct 18, 2009 4:20 pm

Thank you. But the thing that I want is more complicated. I want to serialize the objects that I wrote. For example Person class with name,id,address properties. May be includes an an array list PhoneNumbers.
burakkilic
Developer
Developer
 
Posts: 37
Joined: Thu Oct 01, 2009 10:24 am

Top

Postby padde » Sun Oct 18, 2009 7:34 pm

Small example that shows how to do it....

JsonTest.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.padde.jsontest;
  2.  
  3. import android.app.Activity;
  4. import android.location.LocationManager;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7.  
  8. public class JsonTest extends Activity {
  9.     public void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.main);
  12.        
  13.         MyPerson p = new MyPerson("Max Muster", "Musterstrasse 1");
  14.         p.addPhone("0123456789", 1);
  15.         p.addPhone("9876543210", 2);
  16.        
  17.         p = new MyPerson("Maxine Muster", "Musterstrasse 1a");
  18.         p.addPhone("0123498765", 0);
  19.        
  20.         String json = MyPerson.AllToJSON();
  21.        
  22.         Log.v("toString", MyPerson.AllToString());
  23.         Log.v("toJSON", json);
  24.        
  25.         p = null;
  26.         MyPerson.reset();
  27.         Log.v("reset", MyPerson.AllToString());
  28.        
  29.         MyPerson.fromJSON(json);
  30.        
  31.         Log.v("fromJSON", MyPerson.AllToString());
  32.     }
  33. }
  34.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


MyPerson.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.padde.gpstest;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import org.json.JSONArray;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8.  
  9. class MyPhoneNumber {
  10.     private String number;
  11.     private int type;
  12.    
  13.     public MyPhoneNumber(String n, int t) {
  14.         this.number = n;
  15.         this.type = t;
  16.     }
  17.  
  18.     public String toJSON() {
  19.         JSONObject nr = new JSONObject();
  20.         try {
  21.             nr.put("number", this.number);
  22.             nr.put("type", this.type);
  23.             return nr.toString();
  24.         } catch (JSONException e) {}
  25.         return null;
  26.     }
  27.    
  28.     public String toString() {
  29.         return "Number -> "+this.number+"ntType   -> "+this.type;
  30.     }
  31. }
  32.  
  33.  
  34. public class MyPerson {
  35.     private static int count = 0;
  36.     private static ArrayList<MyPerson> persons = new ArrayList<MyPerson>();
  37.    
  38.     private int id;
  39.     private String name;
  40.     private String adress;
  41.     private ArrayList<MyPhoneNumber> numbers;
  42.    
  43.     public MyPerson(String n, String a) {
  44.         this.name = n;
  45.         this.adress = a;
  46.         this.numbers = new ArrayList<MyPhoneNumber>();
  47.         this.id = MyPerson.count++;
  48.         MyPerson.persons.add(this);
  49.     }
  50.    
  51.     public void addPhone(String number, int type) {
  52.         numbers.add(new MyPhoneNumber(number,type));
  53.     }
  54.    
  55.     public String toJSON() {
  56.         JSONObject mp = new JSONObject();
  57.         JSONArray nrs = new JSONArray();
  58.         for(MyPhoneNumber mpnr : this.numbers) {
  59.             try {
  60.                 nrs.put(new JSONObject(mpnr.toJSON()));
  61.             } catch (JSONException e) { }
  62.         }
  63.         try {
  64.             mp.put("id", this.id);
  65.             mp.put("name", this.name);
  66.             mp.put("adress", this.adress);
  67.             mp.put("numbers", nrs);
  68.             return mp.toString();
  69.         } catch (JSONException e) {}
  70.         return null;
  71.     }
  72.    
  73.     public String toString() {
  74.         StringBuffer sb = new StringBuffer();
  75.         sb.append("Id      -> "+this.id+"n");
  76.         sb.append("Name    -> "+this.name+"n");
  77.         sb.append("Adress  -> "+this.adress+"n");
  78.         sb.append("Numbers:   n");
  79.         for(MyPhoneNumber mpnr : this.numbers) {
  80.             sb.append("t"+mpnr.toString()+"n");
  81.         }
  82.         return sb.toString();
  83.     }
  84.    
  85.     public static String AllToJSON() {
  86.         JSONArray jpersons = new JSONArray();
  87.         for(MyPerson p : MyPerson.persons) {
  88.             try {
  89.                 jpersons.put(new JSONObject(p.toJSON()));
  90.             } catch (JSONException e) {}
  91.         }
  92.         return jpersons.toString();
  93.     }
  94.    
  95.     public static String AllToString() {
  96.         StringBuffer sb = new StringBuffer();
  97.         for(MyPerson p : MyPerson.persons) {
  98.             sb.append(p.toString()+"n");
  99.         }
  100.         return (sb.length() == 0) ? "null" : sb.toString();
  101.     }
  102.    
  103.     public static void reset() {
  104.         MyPerson.count = 0;
  105.         MyPerson.persons.clear();
  106.     }
  107.    
  108.     public static void fromJSON(String json) {
  109.         try {
  110.             JSONArray persons = new JSONArray(json);
  111.             JSONArray numbers;
  112.             JSONObject person;
  113.             JSONObject number;
  114.             MyPerson mp;
  115.             for(int i=0; i < persons.length(); i++) {
  116.                 person = persons.getJSONObject(i);
  117.                 mp = new MyPerson(person.getString("name"), person.getString("adress"));
  118.                 numbers = person.getJSONArray("numbers");
  119.                 for(int x=0; x < numbers.length(); x++) {
  120.                     number = numbers.getJSONObject(x);
  121.                     mp.addPhone(number.getString("number"), number.getInt("type"));
  122.                 }
  123.             }
  124.         } catch (JSONException e) {}
  125.     }
  126. }
  127.  
Parsed in 0.048 seconds, using GeSHi 1.0.8.4


Output
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. toString(1857): Id      -> 0
  2. toString(1857): Name    -> Max Mustermann
  3. toString(1857): Adress  -> Mustermannstrasse 1
  4. toString(1857): Numbers:  
  5. toString(1857):     Number -> 0123456789
  6. toString(1857):     Type   -> 1
  7. toString(1857):     Number -> 9876543210
  8. toString(1857):     Type   -> 2
  9. toString(1857): Id      -> 1
  10. toString(1857): Name    -> Maxine Mustermann
  11. toString(1857): Adress  -> Mustermannstrasse 1a
  12. toString(1857): Numbers:  
  13. toString(1857):     Number -> 0123498765
  14. toString(1857):     Type   -> 0
  15. toJSON(1857): [{"adress":"Mustermannstrasse 1","numbers":[{"number":"0123456789","type":1},{"number":"9876543210","type":2}],"id":0,"name":"Max Mustermann"},{"adress":"Mustermannstrasse 1a","numbers":[{"number":"0123498765","type":0}],"id":1,"name":"Maxine Mustermann"}]
  16. reset(1857): null
  17. fromJSON(1857): Id      -> 0
  18. fromJSON(1857): Name    -> Max Mustermann
  19. fromJSON(1857): Adress  -> Mustermannstrasse 1
  20. fromJSON(1857): Numbers:  
  21. fromJSON(1857):     Number -> 0123456789
  22. fromJSON(1857):     Type   -> 1
  23. fromJSON(1857):     Number -> 9876543210
  24. fromJSON(1857):     Type   -> 2
  25. fromJSON(1857): Id      -> 1
  26. fromJSON(1857): Name    -> Maxine Mustermann
  27. fromJSON(1857): Adress  -> Mustermannstrasse 1a
  28. fromJSON(1857): Numbers:  
  29. fromJSON(1857):     Number -> 0123498765
  30. fromJSON(1857):     Type   -> 0
  31.  
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


maybe not the nicest code... but it should be enough to get the idea about that json stuff.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

i still don't understand

Postby avicohen33 » Tue May 04, 2010 7:37 pm

hi,
i'm trying to write an android client that receives and sends objects to pc server. I tried to send my object from pc to android emulator using serialization and i get an exception in android emulator: Class not found.
i still don't understand: is there a problem with serialization in android or android emulator?
what is the solution for this problem: JSON??

thanks
avicohen33
Freshman
Freshman
 
Posts: 4
Joined: Mon Apr 05, 2010 12:58 pm

Top
Previous

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 24 guests