Why is my JsonObjectRequest not working?

I have been doing this for several days. Can not understand. Android Studio will not allow me to compile it with this error. So, I have this application, where I have two tabs and two fragments. One fragment is called new, and this fragment extracts json. But I could not do it right. I uploaded an image, what the error looks like, and class files. Could you help me?

Error: "Cannot resolve JsonObjectRequest constructor (int, java.lang.String, null .......)

The error

new_fragment.java

public class new_fragment extends Fragment { private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; private String mParam1; private String mParam2; private VolleySingleton volleySingleton; private ImageLoader imageLoader; private RequestQueue requestQueue; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() !=null){ mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } volleySingleton = VolleySingleton.getsInstance(); requestQueue = volleySingleton.getRequestQueue(); RequestQueue requestQueue = VolleySingleton.getsInstance().getRequestQueue(); JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,"http://10.0.8.152/json/new.json",null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { System.out.println(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); requestQueue.add(request); } public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedIntanceState) { setHasOptionsMenu(true); View layout = inflater.inflate(R.layout.new_fragment, container, false); return layout; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { // TODO Auto-generated method stub super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.ref_menu, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // handle item selection switch (item.getItemId()) { case R.id.refreshico: // do s.th. return true; default: return super.onOptionsItemSelected(item); } } } 

VolleySingleton

 public class VolleySingleton { private static VolleySingleton sInstance = null; private ImageLoader mImageLoader; private RequestQueue mRequestQueue; private VolleySingleton(){ mRequestQueue = Volley.newRequestQueue(appClass.getAppContext()); mImageLoader = new ImageLoader(mRequestQueue,new ImageLoader.ImageCache() { private LruCache<String, Bitmap> cache = new LruCache<>((int)(Runtime.getRuntime().maxMemory()/1024)/8); @Override public Bitmap getBitmap(String url) { return cache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { cache.put(url, bitmap); } }); } public static VolleySingleton getsInstance(){ if(sInstance==null){ sInstance = new VolleySingleton(); } return sInstance; } public RequestQueue getRequestQueue(){ return mRequestQueue; } public ImageLoader getImageLoader(){ return mImageLoader; } } 
+7
java android
source share
6 answers

cast (String) null.

 JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,"http://10.0.8.152/json/new.json",(String)null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { System.out.println(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); 
+24
source share

new JsonObjectRequest accepts the third parameter as String requestBody

pass null to string

or you can make an empty string as String rBody = null; and then pass rBody as the third parameter

 JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, "http://10.0.8.152/json/new.json", (String) null, // here new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { System.out.println(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); requestQueue.add(request); 
+9
source share

Well, I see the situation, and this happens because, as your mistake says, the constructor does not exist.

If you use JsonObjectRequest by default and want to use the Get method, you do not need to send a null parameter, you just have to send it as follows:

Change this:

 JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,"http://10.0.8.152/json/new.json",null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { System.out.println(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); requestQueue.add(request); 

For this:

 JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,"http://10.0.8.152/json/new.json", new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { System.out.println(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); requestQueue.add(request); 

As you can see, I just delete the parameter for JsonObject, because the Get method is the constructor that agrees that you are not sending JsonObject.

Another solution is to create your own JsonObjectRequest and configure it to accept such values.

Sincerely.

+3
source share

For anyone who is trying to implement this right now: Google has changed the jsonObjectRequest constructor: now you no longer use this type of request, instead specify the URL first and then null as the second parameter if you want to receive a request for receipt. eg:.

  JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://www.aaa.com/getJSON/dummyMeldung.json", null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { JSONArray jsonArray = response.getJSONArray("meldung"); for (int i = 0; i <= jsonArray.length(); i++) { JSONObject meldung = jsonArray.getJSONObject(i); String comment = meldung.getString("comment"); Log.d("ausgabe", comment); } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); 
+1
source share

There is an ambiguous constructor to solve which use this code:

  JSONObject jsonObject = null; JsonObjectRequest request = new JsonObjectRequest( Request.Method.GET, "Your url", jsonObject, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); 
0
source share

The problem associated with the version of volleyball. In your build.gradle file you should have

 dependencies { ...... compile 'com.mcxiaoke.volley:library:1.0.0' ..... } 
0
source share

All Articles