【问题标题】:Multiple request at android volleyandroid volley 的多个请求
【发布时间】:2017-01-04 18:45:31
【问题描述】:

我发出多个凌空请求时出错。 首先,我要求登录,它工作正常。在登录过程之后,主活动在应用程序中打开。当我在这里提出新请求时,该请求被添加到队列中。但是请求没有被执行。这是什么原因?

这段代码是登录功能:

private void LoginUser(final String email, final String password) {

    String tag_string_req = "request_register";

    pDialog.setMessage("Giriş Yapılıyor ...");
    showDialog();

    StringRequest strReq = new StringRequest(Request.Method.POST,
            URL_LOGIN, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");
                if (!error) {
                    JSONObject user = jObj.getJSONObject("user");
                    int userId = user.getInt("UserId");
                    int uId = user.getInt("UId");


                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra("UserId", userId);
                    intent.putExtra("UId", uId);
                    startActivity(intent);
                    finish();
                } else {
                    String errorMsg = jObj.getString("message");
                    Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
                    hideDialog();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {

            Map<String, String> params = new HashMap<String, String>();
            params.put("email", email);
            params.put("password", password);

            return params;
        }

    };
    strReq.setShouldCache(false);
    LoginApplication.getInstance().addToRequestQueue(strReq, tag_string_req);
}

此代码是在登录功能后获取有关用户的数据:

public void Sync(final Context context, final int uId)
{
    String tag_string_req = "request_syncUser";
    db1 = new Database(context);

    StringRequest strReq = new StringRequest(Request.Method.POST,
            URL_LOGIN, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //gelen tüm verileri local db ye at
            try {
                SQLiteDatabase db = db1.getWritableDatabase();
                JSONObject jObj = new JSONObject(response);
                JSONArray responseUser = jObj.getJSONArray("user");
                boolean error = jObj.getBoolean("error");
                String message = jObj.getString("message");
                if(!error) {
                    for(int i=0; i<responseUser.length(); i++)
                    {
                        JSONObject user = responseUser.getJSONObject(i);

                        String u_name = user.getString("Name");
                        String u_surname = user.getString("Surname");
                        String u_email = user.getString("Email");
                        String u_password = user.getString("Password");
                        String u_isLogin = user.getString("isLogin");
                        String u_isSync = user.getString("isSync");
                        int u_uId = user.getInt("UId");

                        ContentValues cv = new ContentValues();
                        cv.put("Name", u_name);
                        cv.put("Surname", u_surname);
                        cv.put("Email", u_email);
                        cv.put("Password", u_password);
                        cv.put("isLogin", u_isLogin);
                        cv.put("isSync", u_isSync);
                        cv.put("UId", u_uId);

                        db.insertOrThrow("user", null, cv);

                        Toast.makeText(context, message ,Toast.LENGTH_LONG).show();
                        Toast.makeText(context, u_name + u_surname,Toast.LENGTH_LONG).show();
                    }
                } else {

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(context, error.getMessage(), Toast.LENGTH_LONG).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("uId", String.valueOf(uId));

            return params;
        }
    };
    strReq.setShouldCache(false);
    LoginApplication.getInstance().addToRequestQueue(strReq, tag_string_req);
}

}

【问题讨论】:

  • 发布你的代码和你得到的错误
  • 编辑我的问题。

标签: android json android-studio request android-volley


【解决方案1】:

我们需要看看你的代码吗?

也就是说,我的猜测是您没有以单例模式创建/访问您的队列。完全猜测。总是总是发布代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2016-09-19
    相关资源
    最近更新 更多