gpt4 book ai didi

android - JSON - 与 Android 应用程序一起使用的单个文件

转载 作者:搜寻专家 更新时间:2023-11-01 08:46:49 25 4
gpt4 key购买 nike

我得到的这个应用程序需要屏幕上的四个位置是最新的(标题、地址、日期和图像源​​)。

所以,我想也许我可以组成应用程序将读取的四个不同的 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();
}
}
});

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;

}

最佳答案

是的,从您的服务器获取数据作为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com