PHP での私の JSON 出力は、次のように行われます。
print json_encode(array('rate' => $topcat, 'hometown' => $hometown, 'talk' => $talk));
JSON 出力は、ブラウザで次のように表示されます。
{"rate":"Movies","hometown":"Seattle, WA","talk":"Movies"}
Java/Android では、次のようにします。
protected void onPostExecute(Void v) {
try {
JSONArray jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
Hometown = json_data.getString("hometown");
FavCategory = json_data.getString("rate");
Talk = json_data.getString("talk");
}
} catch (JSONException e1) {
} catch (ParseException e1) {
e1.printStackTrace();
}
if (Hometown.equals("")) {
Hometown = "Not Specified";
}
tvHometown.setText(Hometown);
tvRate.setText(FavCategory);
tvTalk.setText(Talk);
Log.d("Log: ", Hometown + " " + FavCategory + " " + Talk);
}
}
そのログで、私はこれを取得します: Seattle, WA, null, null
誰でも理由がわかりますか?
編集:新しいJavaコード、まだエラーが発生しています:
String homeTown = "", favCategory = "", favTalk = "";
try {
JSONObject jsonData = new JSONObject(result);
homeTown = jsonData.getString("hometown");
favCategory = jsonData.getString("rate");
favTalk = jsonData.getString("talk");
tvHometown.setText(homeTown);
tvRate.setText(favCategory);
tvTalk.setText(favTalk);
} catch (JSONException e1) {
} catch (ParseException e1) {
e1.printStackTrace();
}
例外が発生しています:
02-05 08:51:48.078: E/log_tag(22958): Error in http connection org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray