Volley - Cannot Resolve Constructor "JSONObjectRequest
Asked Answered
U

3

5

I have this issue where I am following

http://www.androidhive.info/2014/08/android-building-free-wallpapers-app-part-2/

and have come to the point where I add a splash screen and am starting the Volley Request.

The code the tutorial says to use when making the JSONObjectRequest is

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>()

but when I type that out it asks me to import Volley library, which then changes it to

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>()

Doing this brings up the error cannot resolve constructor.

The code I have in my SplashActivity.java is:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class SplashActivity extends Activity {
    private static final String TAG = SplashActivity.class.getSimpleName();
    private static final String TAG_FEED = "feed", TAG_ENTRY = "entry",
            TAG_GPHOTO_ID = "gphoto$id", TAG_T = "$t",
            TAG_ALBUM_TITLE = "title";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getActionBar().hide();
    setContentView(R.layout.activity_splash);

    // Picasa request to get list of albums
    String url = AppConst.URL_PICASA_ALBUMS
            .replace("_PICASA_USER_", AppController.getInstance()
                    .getPrefManager().getGoogleUserName());

    Log.d(TAG, "Albums request url: " + url);

    // Preparing volley's json object request

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url,
            null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, "Albums Response: " + response.toString());
            List<Category> albums = new ArrayList<Category>();
            try {
                // Parsing the json response
                JSONArray entry = response.getJSONObject(TAG_FEED)
                        .getJSONArray(TAG_ENTRY);

                // loop through albums nodes and add them to album
                // list
                for (int i = 0; i < entry.length(); i++) {
                    JSONObject albumObj = (JSONObject) entry.get(i);
                    // album id
                    String albumId = albumObj.getJSONObject(
                            TAG_GPHOTO_ID).getString(TAG_T);

                    // album title
                    String albumTitle = albumObj.getJSONObject(
                            TAG_ALBUM_TITLE).getString(TAG_T);

                    Category album = new Category();
                    album.setId(albumId);
                    album.setTitle(albumTitle);

                    // add album to list
                    albums.add(album);

                    Log.d(TAG, "Album Id: " + albumId
                            + ", Album Title: " + albumTitle);
                }

                // Store albums in shared pref
                AppController.getInstance().getPrefManager()
                        .storeCategories(albums);

                // String the main activity
                Intent intent = new Intent(getApplicationContext(),
                        MainActivity.class);
                startActivity(intent);
                // closing spalsh activity
                finish();

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        getString(R.string.msg_unknown_error),
                        Toast.LENGTH_LONG).show();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Volley Error: " + error.getMessage());

            // show error toast
            Toast.makeText(getApplicationContext(),
                    getString(R.string.splash_error),
                    Toast.LENGTH_LONG).show();

            // Unable to fetch albums
            // check for existing Albums data in Shared Preferences
            if (AppController.getInstance().getPrefManager()
                    .getCategories() != null && AppController.getInstance().getPrefManager()
                    .getCategories().size() > 0) {
                // String the main activity
                Intent intent = new Intent(getApplicationContext(),
                        MainActivity.class);
                startActivity(intent);
                // closing spalsh activity
                finish();
            } else {
                // Albums data not present in the shared preferences
                // Launch settings activity, so that user can modify
                // the settings

                Intent i = new Intent(SplashActivity.this,
                        SettingsActivity.class);
                // clear all the activities
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(i);
            }

        }
    });

    // disable the cache for this request, so that it always fetches updated
    // json
    jsonObjReq.setShouldCache(false);

    // Making the request
    AppController.getInstance().addToRequestQueue(jsonObjReq);

}

}

I'm not sure what to do in order to fix this?

Unscrew answered 20/7, 2015 at 11:56 Comment(0)
W
29

Cast your null value like (String)null

For eg:-

   JsonObjectRequest request = new JsonObjectRequest(JsonRequest.Method.POST, url, (String)null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response){
                pDialog.hide();

                Log.d("Reponse", response.toString());

                Toast.makeText(getApplicationContext(), response.optString("name"), Toast.LENGTH_LONG).show();
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.printStackTrace();

                Log.d("Error = ", volleyError.toString());

                pDialog.hide();
            }
        }){
            @Override
            protected Map<String, String> getParams(){
                 return params;
            };
        }
Wobbling answered 1/1, 2016 at 17:18 Comment(0)
C
4

The error is due to the usage of higher volley version. please use

compile 'com.mcxiaoke.volley:library:1.0.0'

in the gradle dependency. It worked out for me, when I got the same problem.

Chlorite answered 14/4, 2016 at 7:51 Comment(0)
P
3

The parameters in your constructor for request is not correct so you are facing the problem. The correct code to create a JsonObjectRequest is

JsonObjectRequest request = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject arg0) {
                // TODO Auto-generated method stub

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {
                // TODO Auto-generated method stub

            }
        });
Prong answered 20/7, 2015 at 11:59 Comment(6)
Thanks for that, but when i type it exactly as you have it, Android Studio Highlights the "Method" and asks me to import com.volley.android.method.Request. Once i do this, it changes the Method.GET to Request.Method.GET? Then errors out the whole block of codeUnscrew
There is no problem Request.Method.GET and Method.GET both means the sameProng
you can also use 0 instead of Method.GET but its not recommended wayProng
since i am not familiar with android studio cant help you more but you can continue your work by replacing the method to 0Prong
thanks but none of these have assisted. I don't think there is any difference between using Android Studio and Eclipse for exampleUnscrew
Just tested this out, i am not sure what changed, but the problem is that the null argument needs to be a string. (String) null as suggested by the next answer, or just plain old "".Anthropology

© 2022 - 2024 — McMap. All rights reserved.