I am trying to make a GET request for some Json data with the "com.android.volley" module's JsonObjectRequest.
The android development page (google's own website) shows the set up like this:
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. It wants, the url 1st and the method 2nd. 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
. 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? 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
. 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. I tried 1.0.0 and 1.1.0, neither solved my problem.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.