화면의 4 개 장소 (제목, 주소, 날짜 및 이미지 소스)를 최신으로 유지해야하는이 앱이 있습니다.JSON - Android 앱과 함께 사용할 단일 파일
그렇기 때문에 응용 프로그램에서 읽을 수있는 4 가지 JSON 파일을 만들 수 있다고 생각하고 응용 프로그램을 변경하고 싶다면 내 서버에있는 JSON 파일을 변경하면됩니다.
이 (file.json) 같은 아마 뭔가 :
{"app": {
"title": "Screen no. 1",
"address": "Sesame Street",
"date": "01-01-2014",
"image": "http://myserver.com/image.jpg"
}}
물론 안드로이드 응용 프로그램 소스의
는 " http://myserver.com/file.json"에서 정보를 얻을 것이다 JSONParser있을 것입니다. 당신은 무엇을 생각합니까? 충분히 좋을 것이거나 더 좋은 (그리고 더 쉬운) 해결책이 있습니까? Google Endpoints에 대해 알기 위해 노력했지만 매우 복잡합니다.EDIT1 : 나는 file.json의 값이 어떻게 든 지금 그것을 읽을 필요하므로 다운로드 할 수 발견 디버그 모드에서 How to parse JSON in Android - 그것은 "알았어 인쇄 : 나는 여기에서 JSONParser 사용자 정의 클래스를 사용하여이 지점에 도착 주소 : "하지만 값없이 :
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;
}
}
"이미지"는 서버의 이미지에 주소를 제공해야합니다.Additionaly, 나는 앱 시작시에 비교 방법을 추가하고 싶습니다. 그래서 업데이트가 없다면 아무 것도 다운로드하지 않을 것입니다. "myDataJson"은 이미 빨간색 파일입니까? –
좋습니다. 위 이미지 문자열을 URL로 만들어 이미지를 다운로드하십시오. 비교에 관해서는 json의 첫 번째 값을 긴 caled "타임 스탬프"로 만들고 json이 변경 될 때마다 현재 서버 시간으로 업데이트하십시오. 그런 다음 다운로드 한 후 값이 변경되었는지 확인한 다음 데이터 만 처리하십시오. 나는 당신에게 아이디어를주기 위해 대답을 업데이트 할 것이다. – Kelevandos
내 질문을 업데이트했습니다. 지금은 기본적으로 작동하도록 노력하고 있습니다. 그 후에는 타임 스탬프를 얻습니다. –