編輯:中級開發
[
{
"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方法將緩存寫入到流中。
收集用戶數據您已經創建了 Activity 主屏幕布局,現在可以創建用戶界面表單來收集數據了。在本例中,您將創建一個 Robotics Club R
簡介: Java™ 語言是 Android 開發人員所選的工具。android 運行時使用自己的虛擬機 Dalvik,這並不是多數程序開發人員使用
可翻頁的產品細節屏幕為增強產品細節屏幕的可用性,我們定義了一個自定義視圖控制器(ProductScrollVIEwController 類)來支持用戶通過翻頁
Android ANR這個錯誤大家並不陌生,但是從Android 2.2開始出錯的ANR信息會自動上傳給Google進行系統分析改進,當然了你的應用ANR錯誤其實保存在