我正在尝试为我的m8服务器取得高分,并且我遇到了一个问题:我无法将JSONArray转换为JSONObject ...我试图改变它们,但它不会工作,我可以'找到一种方法从JSON读取数据而不使用“名称”。
答案 0 :(得分:0)
这对你有帮助。
public class API {
private String endpoint = "https://api.minesca.pe";
private Context context;
private String args;
private HTTPRequest.StatusCode statusCode;
private String jsonString;
public API(Context context, String args) throws JSONException {
this.context = context;
this.args = args;
fetch();
}
private void fetch() throws JSONException {
HTTPRequest httpRequest = NetworkStack.getInstance(context).performRequest(endpoint + args, Request.Method.GET);
statusCode = httpRequest.getStatusCode();
if (statusCode == HTTPRequest.StatusCode.FOUND) {
jsonString = httpRequest.getOutput();
}
}
public JSONObject getJsonObject() {
try {
return new JSONObject(jsonString);
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
public JSONArray getJsonArray() {
try {
return new JSONArray(jsonString);
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
public HTTPRequest.StatusCode getStatusCode() {
return statusCode;
}
}
private JSONObject getJsonObjectFromAPI() throws APIError, JSONException {
API api = new API(context, getAPIEndpoint());
if (api.getStatusCode() == StatusCode.FOUND) {
return api.getJsonObject();
} else {
throw new APIError("Unexpected response from the server.");
}
}
private JSONArray getJsonArrayFromAPI() throws APIError, JSONException {
API api = new API(context, getAPIEndpoint());
if (api.getStatusCode() == StatusCode.FOUND) {
return api.getJsonArray();
} else {
throw new APIError("Unexpected response from the server.");
}
}