0

I'm quite new to Java/Android Studio and am spending the past two days figuring this out, without succes. I keep getting: Cannot resolve constructor 'JsonArrayRequest(int, java.lang.String, java.lang.String, anonymous com.android.volley.Response.Listener)'

On other threads I see answers that suggest to replace the null to a string or cast the null but this doesn't seem to make the trick.

I'm trying to read the JSON Array at https://www.smartvibes.be/profiles/api/profileview.php?id=5073 for instance.

This is my code:

package com.smartvibes.smartbeat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

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

public class profileViewActivity extends AppCompatActivity {
    RequestQueue rs;
    String url,id, nick, age, city, mainpic, numpics, extrapic0, extrapic1, extrapic2, extrapic3, extrapic4, extrapic5;
    TextView profileIntro;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_view);
        Bundle getProfileId = getIntent().getExtras();
        if(getProfileId == null){
            return;
        }
        String profileid = getProfileId.getString("profileid");
        url = "https://www.smartvibes.be/profiles/api/profileview.php?id="+profileid;
        rs = Volley.newRequestQueue(this);
        sendjsonrequest();
        profileIntro = (TextView) findViewById(R.id.profileIntro);
        //profileIntro.setText(profileData.getPnick());
        profileIntro.setText(profileid);
    }

    public void sendjsonrequest(){
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, "", new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                try {

                // Get current json Array
                JSONArray profile = new JSONArray(response);
                        JSONObject jresponse = profile.getJSONObject(0);
                        Toast.makeText(profileViewActivity.this, "test", Toast.LENGTH_LONG).show();
                        nick = jresponse.getString("nick");
                        age = jresponse.getString("age");
                        city = jresponse.getString("city");
                        mainpic = jresponse.getString("mainpic");
                        numpics = jresponse.getString("numpics");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        });
        rs.add(jsonArrayRequest);
    }
}

When trying to run it I get:

error: no suitable constructor found for JsonArrayRequest(int,String,String,<anonymous Listener<JSONArray>>)
constructor JsonArrayRequest.JsonArrayRequest(String,Listener<JSONArray>,ErrorListener) is not applicable
(actual and formal argument lists differ in length)
constructor JsonArrayRequest.JsonArrayRequest(int,String,JSONArray,Listener<JSONArray>,ErrorListener) is not applicable
(actual and formal argument lists differ in length)

I'm probably doing something (or several things) bad... But can't figure out what.

1
  • changing "" to null gives the same error Commented Oct 10, 2018 at 15:16

2 Answers 2

1

You miss Response.ErrorListener()

Use this

 JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
                    Request.Method.GET,
                    url,
                    null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            try {

                 // Get current json Array
                 JSONArray profile = new JSONArray(response);
                          JSONObject jresponse = profile.getJSONObject(0);
                          Toast.makeText(profileViewActivity.this, "test", Toast.LENGTH_LONG).show();
                          nick = jresponse.getString("nick");
                          age = jresponse.getString("age");
                          city = jresponse.getString("city");
                          mainpic = jresponse.getString("mainpic");
                          numpics = jresponse.getString("numpics");

                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
                        }
                    },
                    new Response.ErrorListener(){
                        @Override
                        public void onErrorResponse(VolleyError error){
                            // Do something when error occurred
                            Snackbar.make(
                                    mCLayout,
                                    "Error...",
                                    Snackbar.LENGTH_LONG
                            ).show();
                        }
                    }
            );

Hope it's help

Sign up to request clarification or add additional context in comments.

Comments

1

Everything seems fine in your codes except ErrorListener.

If you check the source of the JsonArrayRequest, you'll notice that it takes another method as the last parameter which handles the errors which you don't have it.

Simply, adding the Response.ErrorListener() right after the onResponse() method will solve the issue.

You can however make it null to ignore errors, which is not recommended.

Take a look: https://android--examples.blogspot.com/2017/02/android-volley-json-array-request.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.