我得到的这个应用程序需要屏幕上的四个位置保持最新(标题、地址、日期和图像来源)。
所以,我想也许我可以编写应用程序将读取的四个不同的 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 在 Debug模式下,我发现要下载 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();
}
}
});
编辑2:我的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:意外的标记。
这是我的 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;
}
}
最佳答案
是的,从服务器获取数据作为 JSON 文件似乎是解决此问题的最佳且最轻量级的方法(尽管您提供的有关数据实际含义的数据很少)。
我建议使用org.json
库,因为它允许你做这样的事情,减少解析时间:
String myDataJson = ... /* Obtain the data here */
long lastChangeTimestamp = ... /* Obtain the last saved timestamp, probably from SharedPrefs */
JSONObject data = new JSOBObject(myDataJson);
long newTimestamp = data.getLong("ts");
if(newTimestamp > lastChangeTimestamp){
String title = data.getString("title");
String address = data.getString("address");
String date = data.getString("date");
String image = data.getString("image");
/* Do somtehing with the newly obtained data and save the new timestamp to SharedPrefs */
}
关于android - JSON - 与 Android 应用程序一起使用的单个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27468834/