編輯:高級開發
[
{
"id": 912345678901,
"text": "How do I write JSON on android?",
"geo": null,
"user": {
"name": "android_newb",
"followers_count": 41
},
{
"id": 912345678902,
"text": "@android_newb just use android.util.JSonWriter!",
"geo": [50.454722, -104.606667],
"user": {
"name": "jesse",
"followers_count": 2
}
}
]}
上面的JSON數據在android 3.0 honeycomb上的寫入代碼為
public void writeJSonStream(OutputStream out, List messages) throws IOException {
JsonWriter writer = new JSonWriter(new OutputStreamWriter(out, "UTF-8")); //android開發網提示這是UTF-8編碼
writer.setIndent(" ");
writeMessagesArray(writer, messages);
writer.close();
public void writeMessagesArray(JSonWriter writer, List messages) throws IOException {
writer.beginArray();
for (Message message : messages) {
writeMessage(writer, message);
}
writer.endArray();
}
public void writeMessage(JSonWriter writer, Message message) throws IOException {
writer.beginObject();
writer.name("id").value(message.getId());
writer.name("text").value(message.getText());
if (message.getGeo() != null) {
writer.name("geo");
writeDoublesArray(writer, message.getGeo());
} else {
writer.name("geo").nullValue();
}
writer.name("user");
writeUser(writer, message.getUser());
writer.endObject();
}
public void writeUser(JSonWriter writer, User user) throws IOException
接上頁
{writer.beginObject();
writer.name("name").value(user.getName());
writer.name("followers_count").value(user.getFollowersCount());
writer.endObject();
}
public void writeDoublesArray(JSonWriter writer, List doubles) throws IOException {
writer.beginArray();
for (Double value : doubles) {
writer.value(value);
}
writer.endArray();
}}
這裡我們可以看到android 3.0開始的JsonWriter類可以很輕松的寫入JSon格式的數據流,整個類的成員有
Public Constructors
JSonWriter(Writer out) //公共構造方法,參數為Writer類這是一個I/O類的對象
Public Methods
JSonWriter beginArray()
Begins encoding a new array.
JSonWriter beginObject()
Begins encoding a new object.
void close()
Flushes and closes this writer and the underlying Writer.
JSonWriter endArray()
Ends encoding the current array.
JSonWriter endObject()
Ends encoding the current object.
void flush()
Ensures all buffered data is written to the underlying Writer and flushes that writer.
boolean isLenIEnt()
Returns true if this writer has relaxed syntax rules.
JSonWriter name(String name)
Encodes the property name.
JSonWriter nullValue()
Encodes null.
void setIndent(String indent)
Sets the indentation string to be repeated for each level of indentation in the encoded document.
void setLenient(boolean lenIEnt)
Configure this writer to relax its syntax rules.
JSonWriter value(double value)
Encodes value.
JSonWriter value(long value)
Encodes value.
JSonWriter value(Number value)
Encodes value.
JSonWriter value(boolean value)
Encodes value.
JSonWriter value(String value)
Encodes value.
我們平時需要注意的就是API Level為honycomb才能調用,同時上面代碼為utf-8編碼,寫入時保證流清空記住調用flush方法將緩存寫入到流中。
對於android DEX文件進行優化,需要注意的一點是DEX文件的結構是緊湊的,但是我們還是要想方設法的進行提高程序的運行速度,我們就仍然需要對DEX文件進行進一步優
盡管蘋果聯合創始人承認:android將取得最終勝利,不過很多人認為過於開放的環境依然不利於android,無差異的應用體驗正在制約Android超過iPhone。日前
但是當用戶選購android手機系統時也難免會注意到手機的操作系統,就不知“安致”這個名字能否被廣大手機用戶認可了,業界猜測該商標為android的中文商標。盡管and
隨著時代的進步科技的發展Dalvik虛擬機的版本也快速的提升與升級,下面我就對升級前和升級後的Dalvik虛擬機系統 ,做一下自己的解析android作為新一代的基於L