Ich habe ein Problem. Mein Code macht zweimal meine Anfrage und ich weiß wirklich nicht warum. Ich ändere die Ausführungszeit und nichts ändert sich. Jemand kann mir helfen? Hier ist der Code, wo ich die Anfrage zu tun:Android StringRequest zweimal ausgeführt
private void registerUser(final String username, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";
StringRequest strReq = new StringRequest(Request.Method.GET,
AppConfig.URL_REGISTER+"?username="+username+"&password="+password, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e(TAG, "Register Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
// User successfully stored in MySQL
// Now store the user in sqlite
String uid = jObj.getString("userId");
JSONObject user = jObj.getJSONObject("user");
String userName = user.getString("userName");
// Inserting row in users table
db.addUser(uid, userName);
// Launch login activity
Intent intent = new Intent(
SignupActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
} else {
// Error occurred in registration. Get the error
// message
errormsg= jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errormsg, Toast.LENGTH_LONG).show();
hideDialog();
}
} catch (JSONException e) {
e.printStackTrace();
hideDialog();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("username", username);;
params.put("password", password);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
if(errormsg == null)
{
Toast.makeText(this,"Vous pouvez maintenant vous connecter",Toast.LENGTH_LONG).show();
}
//limiter le temps d'execution
strReq.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
ANBD ich meine PHP und kein Problem damit versuchen, weil es nur ein einziges Mal infromation in meine Datenbank einfügen. Dies ist das Java, aber ich finde wirklich nicht warum. Ich habe viel Lösung versucht. Ich versuche, eine Lösung für mich zu fordern
Welcher Zuhörer verwendet onClick() oder onTouch()? –
Ich verwende OnClick(), es ist besser, onTouch() zu verwenden? –
folgen Sie diesem Tutorial https://code.utsplus.com/tutorials/an-introduction-to-volley--cms-23800 – altu