我正在向我的數據庫發送信息,但我不知道如何閱讀我從JSON獲得的回覆,只需閱讀新Activity中的「id」即可。你可以給我發個教程,或者類似的東西。 這裏是我的數據驗證。我爲id獲得了0,並且Id是一個字符串。如何讀取發送的JSONStringer到REST/WCF的響應?
//數據驗證到這裏
if (isValid) {
// POST request to <service>/SaveGUID
HttpPost request = new HttpPost(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
JSONStringer via = new JSONStringer().object()
.key("id").value(checkId).key("username")
.value(usern).key("password").value(pass)
.endObject();
Log.i("JSON Object", via.toString());
StringEntity entity = new StringEntity(
via.toString());
Log.i("String Entity", entity.toString());
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
Log.d("WebInvoke", "Saving : "
+ response.getStatusLine().getStatusCode());
SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("id", checkId);
editor.commit();
next();
}
TNX提前。
也發佈了什麼JSON字符串你是在響應從服務器獲取 –
http://www.androidhive.info/2012/01/android-json-parsing-教程/ – Nermeen
12-06 13:50:58.022:I/JSON對象(32060):{「id」:null,「username」:「ggg」,「password」:「gg」} 12-06 13:50 :58.022:我/字符串實體(32060):[email protected] 12-06 13:50:58.102:D/WebInvoke(32060):Saving:200 – JumboJey