에서 그것을 얻기 후에 나는 JSON 문자열을 다운로드하고 JSON 구문 분석 AsyncTask를 가지고 :안드로이드 2.3에서 구문 분석 JSON이 실패 서버
private class GetHttpData extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
String result = null;
try {
result = makeHttpRequest(HOST + "/menu_test.php");
Log.v(TAG, result);
jsonToDB(result);
} catch (Exception e) {
Log.e(TAG, "", e);
}
return null;
}
}
makeHttpRequest을()입니다 :
public String makeHttpRequest(String Url) throws HttpException, IOException {
String data = null;
HttpPost post = new HttpPost(Url);
HttpParams httpParams = new BasicHttpParams();
// setting some params ...
HttpClient client = new DefaultHttpClient(httpParams);
try {
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
data = EntityUtils.toString(entity, "UTF-8"); // #1
} else {
throw new HttpException();
}
} catch (HttpException e) {
throw e;
} catch (ConnectTimeoutException e) {
throw e;
} catch (ClientProtocolException e) {
throw e;
} catch (IOException e) {
throw e;
} finally {
client.getConnectionManager().shutdown();
}
return data;
}
jsonToDB이입니다 :
private void jsonToDB(String json) throws JSONException,
SQLiteException {
try {
JSONArray jArray = new JSONArray(json);
int jLength = jArray.length();
for (int i = 0; i < jLength; i++) {
JSONObject category = jArray.getJSONObject(i);
JSONArray dishes = category.getJSONArray(JSON_CATEGORY_DISHES);
int dishesLength = dishes.length();
for (int j = 0; j < dishesLength; j++) {
JSONObject dish = dishes.getJSONObject(j);
Log.v(TAG, dish.getString(JSON_DISH_NUMBER));
}
}
} catch (JSONException e) {
Log.e(TAG, "Error parsing JSON data" + e.toString());
throw e;
}
}
그리고 menu_test.php 인쇄이 라인 :
01 23,516,[ {"catnumber":0,
"catname":"Japanese",
"dishes":[ { "number":"39",
"name":"sushi",
"content":"rice and fish",
"price":6.99,
"img":"imgurl"} ]
} ]
모든 안드로이드 4.0 +에서 잘 작동하지만 2.3에서 나는 이상한 행동을 얻을 : 기호는 I도 복사 할 수 없음을 전면에 추가됩니다
08-02 15:24:22.675: V/test(947): ?[{"catnumber":0,"catname":"Japanese","dishes": [{"number":"39","name":"sushi","content":"rice and fish","price":6.99,"img":"imgurl"}]}]
08-02 15:24:22.675: E/test(947): Error parsing JSON dataorg.json.JSONException: Value ? of type java.lang.String cannot be converted to JSONArray
합니다. 내가 makeHttpRequest에서 선 # 1에서 UTF-8 인코딩을 제거 할 경우() 더 jiberish는 전면에 추가됩니다 :
while(!json.startsWith("[")) {
json = json.substring(1);
}
하지만 여전히를 :
08-02 15:27:58.914: V/test(981): [{"catnumber":0,"catname":"Japanese","dishes":[{"number":"39","name":"sushi","content":"rice and fish","price":6.99,"img":"imgurl"}]}]
내가 쉽게 추가하여이 문제를 해결 할 수 있습니다 : 왜이 hapenning 무엇입니까? 어떻게 해결할 수 있습니까? 왜 그것은 안드로이드 2.3에서만 일어 났을까요?
작동하지 않는 것 같습니다. – VM4
[]에서 다른 illeagal charset이 있는지 확인 했습니까? 어쩌면 서버에서 인식 할 수없는 문자 세트가있을 수 있습니다. – evolever