在我的android應用程序中,我必須使用來自服務器的JSON來在應用程序中進行特定的調整。 我試圖實現是讀取此json並將所有值存儲到本地變量中以執行應用程序內的操作。解析JSON到cofigure android應用程序
JSON從服務器:
[
{
"sett": " ",
"glHdr": {
"sm": [ ],
"scleHPad": false,
"st": "sbsm"
},
"colrBG": [
23,
105,
184,
100
],
"colrTB": [
0,
0,
0,
0
],
"colrTR": [
255,
255,
255,
100
],
"glFtr": {
"icoSz": "icoSzN",
"sm": [ ],
"scleHPad": false,
"gvNR": 3,
"gvHIT": false,
"gvNC": 3,
"st": "igsm"
},
"statBr": true
},
{
"sm": [
{
"tbico": "b43-jeep.png",
"t": "Welcome!",
"w": "http://google.com/start",
"st": "w",
"wTBL": "wTBLN"
},
{
"t": "Content screen title",
"f": "Eltec%20Spec%20sheet%20Gim%20RD30W.pdf",
"st": "f"
},
{
"tbico": "109-chicken.png",
"t": "Sub menu",
"sm": [
{
"t": "Screen 1",
"st": "f"
},
{
"t": "Screen 2",
"w": "Http://google.com",
"st": "w",
"wTBL": "wTBLT"
}
],
"st": "sm"
},
{
"st": "f"
}
],
"st": "tbm"
}
]
解析這個我已經創建瞭解析JSON類,並試圖想出方法來讀取,這JSON在我的應用程序商店的價值。
功能來做到這一點:
public void doScanAppConfigJson(){
private static final String = TAG_TITLE;
private static final String = TAG_WEB_ADDRESS;
private static final String = TAG_SCREEN_TYPE;
private static final String = TAG_FILENAME;
JSONArray appConfig = null;
// Function for looping json object via ParseJson class.
//Creating JSON Parser instance
JSONParser jParser = new JSONParser();
//Getting json strings from url
JSONObject jsonObject = jParser.getJSONFromUrl(url);
try{
//Getting array of settings
appConfig = jsonObject.getJSONArray(ConfigConstants.TABLE_VIEW_SUB_MENU_CONFIG);
//loop throw all the objects under -sm[]
for (int i = 0; i < appConfig.length(); i++){
JSONObject sm = appConfig.getJSONObject(i);
//Now store each of this json in local constant var.
String tabTitle = sm.getString(TAG_TITLE);
String webAddress = sm.getString(TAG_WEB_ADDRESS);
String screenType = sm.getString(TAG_SCREEN_TYPE);
String fileName = sm.getString(TAG_FILENAME);
}
}catch (JSONException e){
e.printStackTrace();
}
}
getJSONFromUrl方法:
public JSONObject getJSONFromUrl(String url) {
//Global authentication for link username and password.
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("username", "password".toCharArray());
}
});
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
並即時得到錯誤:settingsjava.lang.NullPointException
在這一點,但我有定義瓦爾,請一些人告訴我怎麼做我上面分析JSON。我在正確的軌道上實現答案。我的問題?
你必須張貼的錯誤,否則我們幫不了你。 – 2013-03-26 03:54:33
http://codeworkx.de/wordpress/wp-content/uploads/2011/12/jacoj.jpg – 2013-03-26 03:58:08
plz看看更新的問題 – star18bit 2013-03-26 04:05:22