26
KAIWEN – HAN – DANIEL – BIHAN COMP523 NOV. 2011 Android Data Exchange

Android data exchange

Embed Size (px)

Citation preview

Page 1: Android data exchange

KAIWEN – HAN – DANIEL – BIHANCOMP523NOV. 2011

Android Data Exchange

Page 2: Android data exchange

Android Data Exchange

Android EnviromentSQL->PHPPHP->JsonJSON ParsingData sharing in Android

Page 3: Android data exchange
Page 4: Android data exchange

Android Programs

Page 5: Android data exchange

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.

Page 6: Android data exchange

Activities

Views in combination with XML are used to organize user interfaces within activities.

Views are objects which control spaces of the screen.

Page 7: Android data exchange

Intents

Intents are messages that activate other activities, services, or content providers.

Intents can be interpreted by different activities (ex. Different gallery browsers).

Page 8: Android data exchange

Data Exchange

Between cellphone and other devices(cellphone,computer…)

Between cellphone and web

Internal programs in cellphone

Page 9: Android data exchange

Data Exchange

Data exchange between cellphone/device Mobile wireless communication(Data,Voice)

Bluetooth (range:100m)

NFC(Near-field communications) (range:10cm)

Page 10: Android data exchange

Data Exchange

Between cellphone and web:

Page 11: Android data exchange

Data Exchange

Web data access/processing

Page 12: Android data exchange

Data format

Data package format

Page 13: Android data exchange

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");  

Page 14: Android data exchange

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();

?>

Page 15: Android data exchange

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

Page 16: Android data exchange

Classes

HttpClient httpclient ;HttpPost httppost;HttpResponse response;

Page 17: Android data exchange

Usage

Send HTTP requestHandle HTTP response

Page 18: Android data exchange

GSON

Easy to use library to handle JSON dataExample:

Gson gson = new Gson();

final topicList listTopic= gson.fromJson(inputJson, topicList.class);

Return: private int topicCount; private List<Topic> topicList; //Topic is another object

Page 19: Android data exchange

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 } ]

}

Page 20: Android data exchange

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)

Page 21: Android data exchange

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

Page 22: Android data exchange

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

Page 23: Android data exchange

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

Page 24: Android data exchange

Sharing Complex Objects

The android.app.Application 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();

Page 25: Android data exchange

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

Page 26: Android data exchange

Thanks