Multi Threading
Multi Threading
q Each thread has its own call stack. The call stack is used on
method calling, parameter passing, and storage for the called
method’s local variables.
results
2 secs
later
Params: the type of the input parameters sent to the task at execuBon.
Progress: the type of the progress units published during the background computaBon.
❑ doInBackground
■ the main operation. Write your heavy operation here.
❑ onProgressUpdate
■ Indication to the user on the current progress. It is
invoked every time publishProgress() is called.
3!
4!
doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing.
This step is used to perform background computation that can take a long time. The parameters of the asynchronous
task are passed to this step. The result of the computation must be returned by this step and will be passed back to
the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These
values are published on the UI thread, in the onProgressUpdate(Progress...) step.
PENDING! ASYNCTASK.STATUS.PENDING!
RUNNING! ASYNCTASK.STATUS.RUNNING!
FINISHED!
ASYNCTASK.STATUS.FINISHED!
■ What format?
❑ Examples:
■ A Square
■ A Dictionary
■ A Donation…
❑ Disadvantages
■ Takes more memory than necessary
❑ Alternative? - use a standardized system -- JSON
■ Makes the information more portable
❑ Text-based.
❑ Light-weight.
❑ Easy to parse.
❑ http://jsonschemalint.com/
❑ http://json-schema.org/implementations
❑ http://en.wikipedia.org/wiki/
JSON#Schema_and_Metadata
❑ Array
■ Ordered list of values
s trin g
num ber
o b je c t
a rra y
true
false
null
q u o ta tio n m a rk
\ "
re v e rs e s o lid u s
\
s o lid u s
/
bac k s pac e
b
fo rm fe e d
f
n e w lin e
n
c a rria g e re tu rn
r
h o riz o n ta l ta b
t
u 4 h e x a d e c im a l d ig its
0 . d ig it
-
d ig it
e
1 - 9
d ig it E
+
d ig it
-
{ s trin g : v a lu e }
{
"_id":"560515770f76130300c69953",
"usertoken":"11343761234567808125",
"paymenttype":"PayPal",
"__v":0,
"upvotes":0,
"amount":1999
}!
Android Multithreading & Networking
70!
Array
❑ Arrays are ordered sequences of values
❑ Arrays are wrapped in []
❑ , separates values
❑ JSON does not talk about indexing.
■ An implementation can start array indexing at 0 or 1.
[ v a lu e ]
[
{"_id":"560515770f76130300c69953","usertoken":"11343761234567808125","
paymenttype":"PayPal","__v":0,"upvotes":0,"amount":1999},
{"_id":"56125240421892030048403d","usertoken":"11343761234567808125","
paymenttype":"PayPal","__v":0,"upvotes":5,"amount":1234},
{"_id":"5627620ac9e9e303005b113c","usertoken":"11343761234567808125","
paymenttype":"Direct","__v":0,"upvotes":2,"amount":1001}
]
Android Multithreading & Networking
72!
MIME Media Type & Character Encoding
❑ application/json
❑ Strictly UNICODE.
❑ Default: UTF-8.
Gson is a Java library that can be used to convert Java Objects into
their JSON representation. It can also be used to convert a JSON string
to an equivalent Java object. Gson is an open-source project hosted at
http://code.google.com/p/google-gson.
Gson can work with arbitrary Java objects including pre-existing objects
that you do not have source-code of.
[1] http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm!
Boeing747.html!
cache coherence.
■ Support for request prioritization.
■ Cancellation request API. You can cancel a single request, or you can set blocks
■ Strong ordering that makes it easy to correctly populate your UI with data fetched