如何在Volley库中使用POST和GET方法?

时间:2015-11-15 09:39:34

标签: java android json android-volley

我正在使用volley进行登录身份验证,我使用JSON将值传递给url。 是正确的方法吗?如果没有请告诉我如何在排球库中使用POST和GET方法。我希望通过将字符串传递给网址来使用排球库进行身份验证

 String loginurl = "http://www.souqalkhaleejia.com/webapis/login.php?email="+user+"&password="+pass;
        Log.i("logurl", loginurl);
        JsonObjectRequest loginreq = new JsonObjectRequest(Request.Method.POST, loginurl, (String) null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getString("status").equals("Success")) {
                        String lomsg = response.getString("message");
                        String userid = response.getString("userid");
                        loginsession = new Session(getApplicationContext());
                        loginsession.createLoginSession(user, pass);
                        logineditor.putString("uid", userid);
                        logineditor.commit();
                        if (rememberme.isChecked()) {
                            logineditor.putBoolean("saveboolean", true);
                            logineditor.putString("uname", user);
                            logineditor.putString("pass", pass);
                            logineditor.commit();
                        } else {
                            logineditor.clear();
                            logineditor.commit();
                        }
                        Intent lognext = new Intent(MainActivity.this, Homescreen.class);
                        startActivity(lognext);
                        Toast.makeText(MainActivity.this, ""+lomsg,Toast.LENGTH_SHORT).show();
                    } else {
                        String errmsg = response.getString("message");
                        Toast.makeText(MainActivity.this,""+errmsg,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, "" + error,Toast.LENGTH_SHORT).show();
            }
        });
        AppController.getInstance().addToRequestQueue(loginreq);
    }

1 个答案:

答案 0 :(得分:0)

你应该通过这样的参数

RequestQueue queue = Volley.newRequestQueue(context);
    Map<String, String> jsonParams = new HashMap<String, String>();
    jsonParams.put("userId", UserSingleton.getInstance(context).getUserId());
    //jsonParams.put("userId", alert.getUser().getUserId());

    // Request a string response from the provided URL.
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL , new JSONObject(jsonParams), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            //Response OK


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //Error
        }
    });