G2 and json parsing
I am currently developing an android app for 1.6 that parses a json file received from a server and then displays the results in a listview. So far all of my beta testers have had no issues except for one who owns an HTC G2 running on Android 2.2. He said that he does not see anything on his listview, making me think that there was some problem with the json retrieving or parsing. He also said that he did not have any internet issues while running the app. Even my "refresh" option does not solve his problem. I am wondering if there is anything particularly different about the G2 that would cause this. Here is the code where I deal with the JSON.
First I use an OAuth client to get the file
OAuthMessage request = accessor.newRequestMessage(method, url, parameters);
response = client.invoke(request, OAuthClient.ParameterStyle.BODY);
if (("application/json; charset=utf-8").equals(response.getBodyType())) {
return response.readBodyAsString();
I also display a toast if there was an issue with the json fetching
if (result instanceof Exception) {
// If failed during refreshing vouchers, show message to user
if (USER_REFRESH.equals(flag) || SYSTEM_REFRESH.equals(flag)) {
((ListVouchers)context).getProgressDialog().cancel();
Toast.makeText(context, "Could not load. Try again later.", Toast.LENGTH_LONG).show();
}
Then I store information from the JSON into a database:
JSONArray jArray = new JSONArray(params[0]);
int jarrayLength =开发者_开发百科 jArray.length();
//Create a set of the ids in the json
for (int i = 0; i < jarrayLength; i++) {
JSONObject jsonObj = jArray.getJSONObject(i);
JSONObject categoryObj = jsonObj.getJSONObject("category");
values.put("categoryName", categoryObj.getString("name"));
values.put("categoryId", categoryObj.getString("id")); }
contentResolver.insert(CONTENT_URI, values);
Does anyone have any idea why a G2 in particular would not do this correctly? The user is reporting no crashes, just that he sees my empty view where there is supposed to be information from the json. The only issue I think would cause this is a lack of space but I don't think the user's phone can't store a few kb of json text. I have done an admin login of his account and it works fine on the phones I use for testing.
Edit:
I have asked the user for a logcat and it seems the problem stems from this error:
org.json.JSONException: Value other of type java.lang.String cannot be converted to JSONArray
which happens on this line, where params[0] is the result from the server:
JSONArray jArray = new JSONArray(params[0]);
Could this have to do with certain phones handling text files differently? It seems that the result is not being detected as a json file
Try replacing this:
JSONArray jArray = new JSONArray(params[0]);
with this:
JSONObject jsonData = new JSONObject(params[0]);
JSONArray nameArray = jsonData.names();
JSONArray jArray = json.toJSONArray(nameArray);
I found the problem. it turns out the httpclient on the person's G2 was giving a content-type without a space in application/json; charset=UTF-8. I fixed this issue by checking for "application/json" instead of of 'application/json; charset=UTF-8'.
精彩评论