Upload
kaiwen-qi
View
3.999
Download
2
Tags:
Embed Size (px)
Citation preview
KAIWEN – HAN – DANIEL – BIHANCOMP523NOV. 2011
Android Data Exchange
Android Data Exchange
Android EnviromentSQL->PHPPHP->JsonJSON ParsingData sharing in Android
Android Programs
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.
Activities
Views in combination with XML are used to organize user interfaces within activities.
Views are objects which control spaces of the screen.
Intents
Intents are messages that activate other activities, services, or content providers.
Intents can be interpreted by different activities (ex. Different gallery browsers).
Data Exchange
Between cellphone and other devices(cellphone,computer…)
Between cellphone and web
Internal programs in cellphone
Data Exchange
Data exchange between cellphone/device Mobile wireless communication(Data,Voice)
Bluetooth (range:100m)
NFC(Near-field communications) (range:10cm)
Data Exchange
Between cellphone and web:
Data Exchange
Web data access/processing
Data format
Data package format
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");
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();
?>
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
Classes
HttpClient httpclient ;HttpPost httppost;HttpResponse response;
Usage
Send HTTP requestHandle HTTP response
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
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 } ]
}
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)
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
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
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
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();
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
Thanks