Android data exchange


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Android data exchange

  1. 1. Android Data Exchange KAIWEN – HAN – DANIEL – BIHAN COMP523 NOV. 2011
  2. 2. Android Data Exchange Android Enviroment SQL->PHP PHP->Json JSON Parsing Data sharing in Android
  3. 3. Android Programs
  4. 4. Activities Activities provide the screen interface of programs with which the user Interacts. Activities have a special type of life-cycle within the Android OS.
  5. 5. Activities Views in combination with XML are used to organize user interfaces within activities. Views are objects which control spaces of the screen.
  6. 6. Intents Intents are messages that activate other activities, services, or content providers. Intents can be interpreted by different activities (ex. Different gallery browsers).
  7. 7. Data Exchange Between cellphone and other devices(cellphone,computer…) Between cellphone and web Internal programs in cellphone
  8. 8. Data Exchange Data exchange between cellphone/device  Mobile wireless communication(Data,Voice)  Bluetooth (range:100m)  NFC(Near-field communications) (range:10cm)
  9. 9. Data Exchange Between cellphone and web:
  10. 10. Data Exchange Web data access/processing
  11. 11. Data format Data package format
  12. 12. JSON Lightweight data-interchange format Format example: {"a":1, "b":2, "c":3, "d":4, "e":5} create a JSON data in PHP: <?php $arr = array(a => 1, b => 2, c => 3, d => 4, e => 5); echo json_encode($arr); ?> Get information from JSON JAVA on android String retSrc = EntityUtils.toString(httpResponse.getEntity()); JSONObject result = new JSONObject( retSrc); String value = result.get("token");
  13. 13. Android-PHP-MySQL Example ---Message List <?php include conn27.php; $q=mysql_query("SELECT * FROM tb_topic where activestatus=1 order by lastUpdateTime DESC"); $qnum=mysql_num_rows($q); while($e=mysql_fetch_assoc($q)) $output[]=$e; $sendout=array("topicCount" =>$qnum,"topicList"=>$output); print(json_encode($sendout)); mysql_close(); ?>
  14. 14. Why PHP-MySQL Free  Easy to find a free host that support them Easy to implement  Get a back-end platform quickly  Library to decode JSON output  Library to send HTTP POST request to the server , receive and parse HTTP response
  15. 15. Classes HttpClient httpclient ; HttpPost httppost; HttpResponse response;
  16. 16. Usage Send HTTP request Handle HTTP response
  17. 17. GSON Easy to use library to handle JSON data Example: Gson gson = new Gson(); final topicList listTopic= gson.fromJson(inputJson, topicList.class); Return:  private int topicCount;  private List<Topic> topicList; //Topic is another object
  18. 18. JSON to Objects{ "topicCount":5, "topicList":[ { "topicId":1, "topicName":"Json topic", "topicDescription":"Gson is awsome", "isStarred":true }, { "topicId":2, "topicName":"Topic 2", "topicDescription":"This is a second test", "isStarred":false } ]}
  19. 19. Sharing Primitive Data Shared Preference  Used to store Persistent key-value pairs  Data persists across sessions (even if application is killed)  Can store: booleans, floats, ints, longs, and strings  getSharedPreferences (String name, int mode)  Mode MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE  getPreferences(int mode)
  20. 20. Sharing Primitive Data  Example:public class login_activity{…String Username=“Feynman”;Boolean isloggedIn=true;SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("username", Username); editor.putString(“loggedIn", isloggedIn ); editor.commit();…}public class get_username{…SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE); String User = sharedPreferences.getString("username", "Not Found"); Boolean logInState= sharedPreferences.getString(" loggedIn”, false); User “Feynman” logInState true…}
  21. 21. Sharing Primitive Data Intent Passing  Pass data from intent to intent via Intent.putExtra()  Data does not persist across sessions  Can implement to pass any Parcelable object
  22. 22. Sharing Primitive Data  Example: Public class login_activity{ … String Usename=“Feynman”; Boolean loggedIn=“true”; Intent i = new Intent(getApplicationContext(), get_username.class); intent.putExtra(“username”, Username); intent.putExtra(“loggedIn”, Username); startActivity(i); …}Public class get_username{public void onCreate(Bundle savedInstanceState) {…Intent data=getIntent();String User=data.getExtras().getString(“username");Boolean LogInState=data.getExtras().getBoolean(“loggedIn"); User “Feynman”}…} LogInState true
  23. 23. Sharing Complex Objects The class  Can be used for complex objects  Is not persistent (not preserved across sessions)  Example public class login_activity extends Application { Boolean isLoggedIn=true; public int getLogInState() { return isLoggedIn; } public void setLogInState(Boolean boo) { this.isLoggedIn = boo; } … } ((login_activity) getApplication()).setLogInState(false); Boolean isLoggedIn=((login_activity)getApplication()).getLogInState();
  24. 24. Sharing Complex Objects Other Methods:  SqLite Database  External Storage  Data is stored on some external storage device (SD card)  World-readable, modifiable when USB mass storage is enabled  Internal Storage  Data is stored on device and is persistant across sessions  Data is private to application  contentProvider  Provides data to other applications
  25. 25. Thanks