gpt4 book ai didi

java - 找不到适合 JsonObjectRequest 的构造函数

转载 作者:太空宇宙 更新时间:2023-11-03 13:18:31 25 4
gpt4 key购买 nike

错误:

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)

我认为问题出在类 makeJsonObjectRequest 上,有人可以帮助我吗?

我的类(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);
}



}

最佳答案

换行

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

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

对于 GET 请求,aJsonObject 可以留空,因为只有当你想向服务器发送数据时才需要它,例如 POST/PUT/PATCH 请求,java doc 对此参数的描述是:

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

关于java - 找不到适合 JsonObjectRequest 的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32119792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com