Java – Could not find a suitable constructor for JsonObjectRequest

Could not find a suitable constructor for JsonObjectRequest… here is a solution to the problem.

Could not find a suitable constructor for JsonObjectRequest

Error:

Error:(164, 40) error: no suitable constructor found for
JsonObjectRequest(int,String,>,) constructor
JsonObjectRequest.JsonObjectRequest(String,JSONObject,Listener,ErrorListener)
is not applicable (actual argument int cannot be converted to String
by method invocation conversion) constructor
JsonObjectRequest.JsonObjectRequest(int,String,JSONObject,Listener,ErrorListener)
is not applicable (actual and formal argument lists differ in length)

I think the problem is with the class makeJsonObjectRequest, can someone help me?

My class:

<!-- language: java -->

public class MainActivity extends ActionBarActivity {

public static String api_pvp_net = ".api.pvp.net/api/lol/";  URL API
     Insira a Sua Key depois do sinal de igual exemplo = "?api_key=sua-key-da-riot"
    public static String DEVELOPMENT_API_KEY = "?api_key=xxxxx-c258-48ab-9f52-af4f52b45e4c";  Key Api RIOT
    // ---------------------------------
    public static String summoner_by_name = "v1.4/summoner/by-name/";  Classe API URL
    public static String aUrl;  Url
    public static String Res_Web;  Resultado do WebBroser
    public static String Nome_Invocador;  Pega o Nome do invocador Do Txt_INVOCADOR

private EditText etNick;
    private String nick;
    private Button btnOk;
    private Spinner rg;
    private invocador inv;
    private TextView tvLevel;
    private TextView Level;
    private TextView Id;
    private static String TAG = MainActivity.class.getSimpleName();
    private String jsonResponse;
    private ProgressDialog pDialog;

ArrayAdapter<String> adapter;

ArrayList<String> arrayInv;

Summoner summoner = new Summoner();

private void preencherInv() {
        arrayInv.add("BR");
        arrayInv.add("EUNE");
        arrayInv.add("EUW");
    }

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);

etNick = (EditText) findViewById(R.id.etNick);
        btnOk = (Button) findViewById(R.id.btnOk);
        rg = (Spinner) findViewById(R.id.spRG);
        tvLevel = (TextView) findViewById(R.id.tvLevel);
        inv = new invocador();
        Level = (TextView) findViewById(R.id.level);
        Id = (TextView) findViewById(R.id.id);

arrayInv = new ArrayList<>();

preencherInv();

adapter = new ArrayAdapter<String>(
                this,
                android. R.layout.simple_dropdown_item_1line,
                arrayInv);

rg.setAdapter(adapter);

btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

inv.setNick(etNick.getText().toString());

inv.setRg((String) rg.getSelectedItem());

RequestQueue mRequestQueue;

 Instantiate the cache
                Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024);  1MB cap

 Set up the network to use HttpURLConnection as the HTTP client.
                BasicNetwork network = new BasicNetwork(new HurlStack());

 Instantiate the RequestQueue with the cache and network.
                mRequestQueue = new RequestQueue(cache, network);

 Start the queue
                mRequestQueue.start();

Pesquisa_invocador();

 Formulate the request and handle the response.
                makeJsonObjectRequest();

}
        });

}

public void Pesquisa_invocador() {

 Eu tiro os espacos da String do _nome por que nas resposta ela vem [sem espacos e Toda minuscula]
        Nome_Invocador = inv.getNick().toLowerCase();
         Monto a URL API [CLASSE DA API] com 2 Parametro [Regiao] [NomeInvocador]  + [CHAVE DE DESENVOLVEDOR]
        aUrl = "https://" + inv.getRg().toLowerCase() + api_pvp_net + inv.getRg().toLowerCase() + "/" + summoner_by_name + Nome_Invocador + DEVELOPMENT_API_KEY; ;
        Abro a URL no Webbroser

}

private void makeJsonObjectRequest() {

showpDialog();

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

@Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());

try {
                     Parsing json object response
                     response will be a json object
                    String id = response.getString("id");
                    String name = response.getString("name");
                    String summonerLevel = response.getString("summonerLevel");

jsonResponse = "";
                    jsonResponse += "Name: " + name + "\n\n";
                    jsonResponse += "Level: " + summonerLevel + "\n\n";
                    jsonResponse += "ID: " + id + "\n\n";

tvLevel.setText(jsonResponse);

} catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
                hidepDialog();
            }
        }, new Response.ErrorListener() {

@Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                 hide the progress dialog
                hidepDialog();
            }
        });

 Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }
    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
         Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
         Handle action bar item clicks here. The action bar will
         automatically handle clicks on the Home/Up button, so long
         as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

return super.onOptionsItemSelected(item);
    }

}

Solution

Line break

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

to

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

For GET requests, aJsonObject can be left blank because it is only needed if you want to send data to the server, such as POST/PUT/PATCH requests, java doc describes this parameter as:

@param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and
indicates no parameters will be posted along with request.

Related Problems and Solutions