【发布时间】:2014-12-14 11:26:26
【问题描述】:
我得到了这个应用程序,它需要屏幕上的四个位置是最新的(标题、地址、日期和图像来源)。
所以,我想也许我可以组成四个不同的 JSON 文件供应用程序读取,如果我想更改应用程序显示的内容,我只需更改服务器上的那些 JSON 文件即可。
可能是这样的(file.json):
{"app": {
"title": "Screen no. 1",
"address": "Sesame Street",
"date": "01-01-2014",
"image": "http://myserver.com/image.jpg"
}}
在 Android 应用程序源中当然会有 JSONParser 从“http://myserver.com/file.json”获取信息。你怎么看 - 会足够好还是有更好(和更容易)的解决方案?我试着去了解 Google Endpoints,但是真的很麻烦。
edit1: 我到了这里,我使用 JSONParser 自定义类:How to parse JSON in Android 在调试模式下,我发现要下载 file.json 中的值,所以我现在必须以某种方式读取它 - 它打印“得到地址:”但没有值:
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
Log.i("ABCDE", "Start Thread");
//JSON
JSONParser jparser = new JSONParser();
JSONObject data = jparser.getJSONFromUrl("http://myserv.com/file.json");
Log.i("AbCDE", "Afer getting JSON");
//JSONObject data = new JSONObject(myDataJson);
String address = "";
try {
address = data.getString("address");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("ABCDE", "Got the address: " + address);
} catch (Exception e) {
e.printStackTrace();
}
}
});
edit2:我的 XML 突然停止工作(它可以很好地验证和制作层次结构树,但不是每次都这样):
{
"party1": {
"title": "Screen no. 1",
"address": "Sesame Street",
"date": "01-01-2014",
"image": "http://myserver.com/image.jpg",
"destination": "somewhere"
},
"party2": {
"title": "Screen no. 2",
"address": "Oak Street",
"date": "01-01-2014",
"image": "http://myserver.com/image.jpg",
"destination": "somewhere"
},
"party3": {
"title": "Screen no. 1",
"address": "Sesame Street",
"date": "01-01-2014",
"image": "http://myserver.com/image.jpg",
"destination": "somewhere"
},
"party4": {
"title": "Screen no. 1",
"address": "Sesame Street",
"date": "01-01-2014",
"image": "http://myserver.com/image.jpg",
"destination": "somewhere"
}
}
JSON 验证器说没问题或 SyntaxError: unexpected token.
这是我的 JSONParser.java 类:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {}
public JSONObject getJSONFromUrl(String url) {
// 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;
}
}
【问题讨论】:
标签: android json android-json