[英]“org.json.JSONObject” missing “Request.Method.GET” and “com.android.volley” JsonObjectRequest arguments appear to out of order
I am trying to make a GET request for some Json data with the "com.android.volley" module's JsonObjectRequest.我正在尝试使用“com.android.volley”模块的 JsonObjectRequest 对一些 Json 数据发出 GET 请求。
The android development page (google's own website) shows the set up like this: 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. 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. It wants, the url 1st and the method 2nd.它想要,url 第一和方法第二。 Shouldn't be a problem, just enter the information in a different order, but there is another problem.应该没有问题,只是按不同的顺序输入信息,但还有一个问题。
It insists that the Method be of org.json.JSONObject
, not com.android.volley
.它坚持该方法是org.json.JSONObject
,而不是com.android.volley
。 BUT, I can't find the Request.Method.GET
from org.json.JSONObject
in the auto drop-down selector in Android Stuido, and I have import import org.json.JSONObject
in my code at the top of the page?但是,我无法在 Android Stuido 的自动下拉选择器中找到来自org.json.JSONObject
的Request.Method.GET
,并且我在我的代码中的第 4 个对象的顶部import org.json.JSONObject
So what the heck!?那到底是什么!? The only Request.Method.GET
I can find is from com.android.volley
, which doesn't seem to be accepted by the JsonObjectRequest
.我能找到的唯一Request.Method.GET
来自com.android.volley
,它似乎不被JsonObjectRequest
接受。 I don't know why.我不知道为什么。
My Code: (pretty much all underlined in red the way it is)我的代码:(几乎所有的都用红色下划线)
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();
}
I also have implementation 'com.android.volley:volley:1.1.1'
in my build dependencies.我的构建依赖项中也有implementation 'com.android.volley:volley:1.1.1'
。 I tried 1.0.0 and 1.1.0, neither solved my problem.我尝试了 1.0.0 和 1.1.0,都没有解决我的问题。
YOu have used你用过
new JsonObjectRequest(getString(R.string.getAddress), Request.Method.GET, null, new Response.Listener<JSONObject>(){...});
when the proper form is:当正确的形式是:
new JsonObjectRequest(Method, url,jsonRequest,listener,errorListener);
or或者
new JsonObjectRequest(url,JSONObject,listener,errorListener);
Try this:尝试这个:
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.