JSON - 单个文件与Android应用使用 [英] JSON - single file to use with android app
问题描述
我得到这个应用程序,需要在屏幕上四个地方将上升到日期(名称,地址,日期和图像源)。
I got this app that requires four places on screen to be up-to-date (title, address, date and image source).
所以,我想,也许我可以只妆四种不同的JSON文件的应用程序会读,如果我想改变什么应用程序是显示我只想改变我有我的服务器上的JSON文件。
So, I thought that maybe I could just makeup four different JSON files that app will read and if I would like to change what app is showing I would just change those JSON files that I'd have on my server.
也许这样的事情(file.json):
Maybe something like this (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 。你认为什么 - 将是不够好,或有没有更好的(更容易)解决方案?我试着去了解谷歌端点,但它真的麻烦了。
and in Android app source of course there would be JSONParser that will get informations from "http://myserver.com/file.json". What do You think - would be that good enough or is there any better (and easier) solution? I tried to get to know Google Endpoints, but it's really cumbersome.
EDIT1:我到这个地步,我用JSONParser定制类从这里:的如何解析JSON在Android中
在调试模式下,我发现从file.json值进行下载,所以我必须在某种程度上,现在读它 - 它打印得到的地址:但没有值:
edit1: I got to this point where I use JSONParser custom class from here: How to parse JSON in Android In debug mode I found values from file.json to be downloaded so I have to read it somehow now - it prints "Got the address: " but without value:
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突然停止工作(它验证,使层次树好,但不是每次):
edit2: my XML suddenly stopped working (it validates and makes hierarchy tree well, but not every time):
{
"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验证器说,这是正确的,或者语法错误:。意外的标记
JSON validators says that it's okay or SyntaxError: unexpected token.
这是我的JSONParser.java类:
This is my JSONParser.java class:
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文件获取数据似乎是解决这个(尽管你所提供的数据什么小数据的最佳和最lighweight方式实际上应该意味着)。
Yes, obtaining data from your server as a JSON file seems to be the best and most lighweight way of solving this (although you provided little data on what the data should actually mean).
我会建议使用 org.json
库,它可以让你做这样的事情,切削时间的解析:
I would suggest using org.json
library, as it will allow you to do something like this, cutting time on the parsing:
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 */
}
这篇关于JSON - 单个文件与Android应用使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!