如何在Volley中使用过帐方法?

时间:2018-10-30 12:02:50

标签: android android-volley

我想知道如何在凌空环境中张贴请求对象

class Request {
    int restId;
    List<Item> items;
}    

class Item{
  int itemId;
  int count;
}

1 个答案:

答案 0 :(得分:0)

OnCreate():

RequestQueue requestQueue = Volley.newRequestQueue(this);

示例发布方法:

StringRequest postRequest = new StringRequest(Request.Method.POST, url1,
                    new Response.Listener<String>()
                    {
                        @Override
                        public void onResponse(String response)
                        {
                            try
                            {
                                System.out.println("response: " + response);
                                JSONObject jsonObject = new JSONObject(response);
                                String success = jsonObject.getString("success");
                                System.out.println("success: " + success);

                                // success-e gore usere info gosterilir
                                if (success.equals("true"))
                                {
                                    Toast.makeText(SignUpActivity.this, R.string.register_success, Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(SignUpActivity.this, SignInActivity.class);
                                    startActivity(intent);
                                }
                                else
                                {
                                    String errorMessage = jsonObject.getString("message");
                                    Toast.makeText(SignUpActivity.this, R.string.register_failed, Toast.LENGTH_SHORT).show();
                                }
                            }
                            catch (JSONException e)
                            {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener()
                    {
                        @Override
                        public void onErrorResponse(VolleyError error)
                        {
                            Toast.makeText(SignUpActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
                        }
                    }
            )
            {
                @Override
                protected Map<String, String> getParams()
                {
                    Map<String, String> params = new HashMap<>();
                    params.put("name", nameText);
                    params.put("login", userNameText);
                    params.put("email", emailText);
                    params.put("password", passwordText);

                    if (!dateText.equals(""))
                        params.put("birthday", dateText);

                    return params;
                }
            };
            requestQueue.add(postRequest);