我收到了API的回复。 但是一旦我到达return命令,我就会收到以下错误:
W/System.err: org.json.JSONException: No value for 0
W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at org.json.JSONObject.getInt(JSONObject.java:478)
W/System.err: at bobbydejong.expandabletest.OrderOverzicht.prepareOrderList(OrderOverzicht.java:65)
W/System.err: at bobbydejong.expandabletest.OrderOverzicht$1.onSuccess(OrderOverzicht.java:37)
W/System.err: at bobbydejong.expandabletest.GetOrderSwagger.onPostExecute(GetOrderSwagger.java:113)
W/System.err: at bobbydejong.expandabletest.GetOrderSwagger.onPostExecute(GetOrderSwagger.java:21)
W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:660)
W/System.err: at android.os.AsyncTask.-wrap1(AsyncTask.java)
它发生在这段代码上:
public class GetItemsSwagger extends AsyncTask<String, Void, String> {
ArrayList<HashMap<String, Object>> list = new ArrayList<>();
String result2;
private OnEventListener<String> mCallBack;
public Exception mException;
private Context context;
HttpURLConnection conn = null;
OutputStream os = null;
InputStream is = null;
String toastMsg = "Failed to get API response.";
String apiKey;
public GetItemsSwagger(Context context, OnEventListener callback) {
this.context = context;
this.mCallBack = callback;
}
@Override
protected String doInBackground(String... strings) {
try {
apiKey = strings[1];
URL url = new URL(strings[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("GET");
conn.setUseCaches(false);
conn.setRequestProperty("apikey", apiKey);
int status = conn.getResponseCode();
if(status >= 400)
is = conn.getErrorStream();
else
is = conn.getInputStream();
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
} else {
toastMsg = "";
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(is)));
String output;
while ((output = br.readLine()) != null) {
toastMsg += output;
}
conn.disconnect();
result2= "";
return toastMsg;
编辑:
[{"orderGroupID":"1","name":"Patat","orderStatusID":"8","orderTo":"2017-02-24 11:15:00","created":"2017-02-24 10:04:21"},{"orderGroupID":"2","name":"Patat","orderStatusID":"8","orderTo":"2017-03-03 11:00:00","created":"2017-03-03 10:06:53"},{"orderGroupID":"3","name":"Patat","orderStatusID":"8","orderTo":"2017-03-10 11:10:00","created":"2017-03-10 10:27:32"},{"orderGroupID":"4","name":"Patat","orderStatusID":"8","orderTo":"2017-03-17 11:00:00","created":"2017-03-17 08:27:48"},{"orderGroupID":"5","name":"Patat","orderStatusID":"8","orderTo":"2017-03-24 11:00:00","created":"2017-03-24 08:58:08"},{"orderGroupID":"6","name":"Patat","orderStatusID":"8","orderTo":"2017-03-31 11:00:00","created":"2017-03-31 09:26:01"},{"orderGroupID":"7","name":"Patat","orderStatusID":"8","orderTo":"2017-04-07 11:00:00","created":"2017-04-07 10:02:52"},{"orderGroupID":"8","name":"Patat","orderStatusID":"8","orderTo":"2017-04-14 11:00:00","created":"2017-04-14 09:40:43"},{"orderGroupID":"9","name":"Patat","orderStatusID":"8","orderTo":"2017-04-21 11:00:00","created":"2017-04-21 10:01:31"},{"orderGroupID":"11","name":"Patat","orderStatusID":"8","orderTo":"2017-04-28 11:00:00","created":"2017-04-28 10:12:45"},{"orderGroupID":"12","name":"Patat","orderStatusID":"8","orderTo":"2017-05-05 11:00:00","created":"2017-05-05 09:39:56"},{"orderGroupID":"13","name":"Patat","orderStatusID":"8","orderTo":"2017-05-12 11:10:00","created":"2017-05-12 09:52:26"},{"orderGroupID":"14","name":"Patat","orderStatusID":"8","orderTo":"2017-05-19 11:00:00","created":"2017-05-19 09:58:37"}]
这是JSON响应
try {
JSONArray jObjArray = new JSONArray(orderResult);
int length = jObjArray.length();
final ArrayList<String> orderList = new ArrayList<String>();
for (int i = 0; i < length; i++) {
HashMap<String, Object> map = new HashMap<>();
JSONObject jObj = jObjArray.getJSONObject(i);
map.put("orderGroupID", jObj.getInt("orderGroupID"));
map.put("0", jObj.getInt("0"));
map.put("name", jObj.getString("name"));
map.put("orderStatusID", jObj.getInt("orderStatusID"));
map.put("2", jObj.getInt("2"));
map.put("orderTo", jObj.getString("orderTo"));
map.put("3", jObj.getString("3"));
map.put("created", jObj.getString("created"));
String orderTo = jObj.getString("orderTo");
String orderGroupID = jObj.getString("orderGroupID");
orderList.add(orderTo+" "+orderGroupID);
这是我操纵代码(不同类)
的地方我期待你的回复,我一直试图在其他帖子上找到这个问题,但似乎这些解决方案不适合我,而不是完全相同的问题。如果我错了,请纠正我,但我是首发!
答案 0 :(得分:0)
首先,&#34; orderGroupID&#34;您的JSON响应中的值是字符串,并且您尝试通过jObj.getInt("orderGroupID"));
访问它。您应该使用jObj.getString("orderGroupID"));
其次,没有任何有价值的钥匙&#34; 0&#34;在您的JSON响应中,以便解析将抛出和异常。
答案 1 :(得分:0)
由于您的orderGroupID
值为string
,只需使用jObj.getString("orderGroupID")
代替jObj.getInt("orderGroupID")
。
试试这个:
try {
JSONArray jObjArray = new JSONArray(orderResult);
int length = jObjArray.length();
final ArrayList<String> orderList = new ArrayList<String>();
for (int i = 0; i < length; i++) {
HashMap<String, Object> map = new HashMap<>();
JSONObject jObj = jObjArray.getJSONObject(i);
map.put("orderGroupID", jObj.getString("orderGroupID"));
map.put("name", jObj.getString("name"));
map.put("orderStatusID", jObj.getString("orderStatusID"));
map.put("orderTo", jObj.getString("orderTo"));
map.put("created", jObj.getString("created"));
..........
.................
}