我正在编写一个简单的Android应用程序,它使用API来获取一些数据但是我收到以下错误:
$('#checkout').on('click', function() { // on clicking the [Checkout] button
try {
// (1) perform validity check on the selected rows
RowData.validityCheck(); // will throw if error is detected
// (2) perform validity check on the #pos_drop selection
var pos_city = $("#pos-drop").val();
if (!pos_city) {
throw new Error('Please select a POS-City from the dropdown list.');
}
// (3) perform any further validity checks here (and throw accordingly)
// Execution will only reach this point if no validity error is encountered.
var link = "mailto:me@example.com" + "?subject=" + encodeURIComponent("Order") + "&body=Location: " + pos_city + '%0D%0A' + RowData.toEmailString(); // check that delimiter.
console.log(link);
window.location.href = link;
}
catch(e) {
console.error(e);
$('#userMessage').text(e.message); // element #userMessage - eg a <span> - needs to exist somewhere on the page
}
});
以下是我的代码:
System.err: org.json.JSONException: No value for genre_ids
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:584)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at com.adeel.android.movieinfoapp.Networking.onPostExecute(Networking.java:72)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at com.adeel.android.movieinfoapp.Networking.onPostExecute(Networking.java:21)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:632)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at android.os.Looper.loop(Looper.java:145)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5832)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
}
我尝试编写的应用使用此JSON。
答案 0 :(得分:2)
您没有正确解析嵌套JsonArray
..
此genre_ids
数组位于results
数组
使用以下代码:
try {
JSONObject jsonObject = new JSONObject(s); //string pass to your onPostExecute()
JSONArray result = jsonObject.getJSONArray("results");
for(int j=0;i<result.length;j++) {
JSONObject inner = result.getJSONObject(j);
JSONArray jsonStrings = inner.getJSONArray("genre_ids");
String strings[] = new String[jsonStrings.length()];
for(int i=0;i<strings.length;i++) {
strings[i] = jsonStrings.getString(i);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
答案 1 :(得分:1)
添加到rafsanahmad007点,如果使用“opt”而不是“get”,则可以获得空值而不是异常
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray resultArray = jsonObject.optJSONArray("results");
if (resultArray == null) {
Log.d("MM", "Json array is null");
return;
}
for (int i = 0; i < resultArray.length(); i++) {
JSONObject resultObj = resultArray.getJSONObject(i);
if (resultObj == null) {
continue;
}
JSONArray jsonStrings = jsonObject.optJSONArray("genre_ids");
if (jsonStrings != null) {
String strings[] = new String[jsonStrings.length()];
for (int j = 0; j < strings.length; j++) {
strings[j] = jsonStrings.optString(j);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}