[英]Android - org.json.simple.JSONObject cannot be cast to org.json.JSONObject
[英]“org.json.JSONObject” missing “Request.Method.GET” and “com.android.volley” JsonObjectRequest arguments appear to out of order
我正在尝试使用“com.android.volley”模块的 JsonObjectRequest 对一些 Json 数据发出 GET 请求。
android 开发页面(google自己的网站)显示设置如下:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { ... } );
The order of the arguments being: 1. Method 2. url etc etc. HOWEVER, when I try this using Android studio, I get an error and it asks for the arguments in a different order. 它想要,url 第一和方法第二。 应该没有问题,只是按不同的顺序输入信息,但还有一个问题。
它坚持该方法是org.json.JSONObject
,而不是com.android.volley
。 但是,我无法在 Android Stuido 的自动下拉选择器中找到来自org.json.JSONObject
的Request.Method.GET
,并且我在我的代码中的第 4 个对象的顶部import org.json.JSONObject
那到底是什么!? 我能找到的唯一Request.Method.GET
来自com.android.volley
,它似乎不被JsonObjectRequest
接受。 我不知道为什么。
我的代码:(几乎所有的都用红色下划线)
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.toolbox.JsonObjectRequest;
import java.io.IOException;
<more code here, but not relevent>
try{
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(getString(R.string.getAddress), Request.Method.GET, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray array = response.getJSONArray("games");
JSONArray releventGames = new JSONArray();
for (int i = 0; i < array.length(); i++) {
JSONObject game = array.getJSONObject(i);
String homeTeam = game.getString("hometeam");
String awayTeam = game.getString("awayteam");
if (homeTeam.contains(teamName) || awayTeam.contains(teamName)) {
releventGames.put(game);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
// queue.add(jsonObjectRequest);
// JSONObjectResquest();
}catch (IOException e){
Log.i("getGames","Failed to Get games");
e.printStackTrace();
}
我的构建依赖项中也有implementation 'com.android.volley:volley:1.1.1'
。 我尝试了 1.0.0 和 1.1.0,都没有解决我的问题。
你用过
new JsonObjectRequest(getString(R.string.getAddress), Request.Method.GET, null, new Response.Listener<JSONObject>(){...});
当正确的形式是:
new JsonObjectRequest(Method, url,jsonRequest,listener,errorListener);
或者
new JsonObjectRequest(url,JSONObject,listener,errorListener);
尝试这个:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "URL", null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
},null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.