Android StringRequest执行两次

时间:2017-06-16 07:04:24

标签: php android json android-volley

我遇到了问题。我的代码做了两次我的请求,我真的不知道为什么。我改变了执行的时间,没有任何改变。有人可以帮助我吗? 这是我执行请求的代码:

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我尝试使用PHP并没有问题因为它只在我的数据库中插入一次信息。这是Java,但我真的不知道为什么。 我尝试了很多解决方案。我试图要求我的解决方案

1 个答案:

答案 0 :(得分:0)

试试这个。

strReq.setRetryPolicy(new DefaultRetryPolicy(
                    (int) TimeUnit.SECONDS.toMillis(20),
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));