編輯:Android開發實例
在Android 3.0中新增了一個JSON寫入類android.util.JsonWriter,使用JsonWriter可以輕松的生成JSON格式的數據,比如下面的JSON數據為
[
{
"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方法將緩存寫入到流中。
JSON代表JavaScript對象符號。它是一個獨立的數據交換格式,是XML的最佳替代品。本章介紹了如何解析JSON文件,並從中提取所需的信息。Android提供了四個
當前比較成熟一點的應用基本上都會在進入應用之顯示一個啟動界面.這個啟動界面或簡單,或復雜,或簡陋,或華麗,用意不同,風格也不同.下面來觀摩幾個流行的應用的啟動界面
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩