在片段

时间:2017-09-14 14:27:41

标签: android android-fragments android-volley

我想创建注册页面。我在SignFragment中添加凌空。我无法连接Json。我给出了带或不带参数的注册数据。请帮我。 这是我的代码。

SignFragment.java

    public class SignFragment extends Fragment {
private EditText editTextFname, editTextLname, editTextMnumber, editTextEmail, editTextPass, editTextCpass;
private String firstName, lastname, mobileNumber, emailId, password, cPassword;
private Button buttonRegister;
public SignUpFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_sign_up, container, false);
    editTextFname=(EditText) view.findViewById(R.id.f_name);
    editTextLname=(EditText) view.findViewById(R.id.l_name);
    editTextMnumber=(EditText) view.findViewById(R.id.user_mobile);
    editTextEmail=(EditText) view.findViewById(R.id.user_email);
    editTextPass=(EditText) view.findViewById(R.id.user_pass);
    editTextCpass=(EditText) view.findViewById(R.id.user_cpass);

    buttonRegister=(Button)view.findViewById(R.id.register_btn);
    buttonRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            userRegister();
        }
    });

    return view;
}

private void userRegister() {

    String url = "...../api/Signup";
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {


                    try {
                        JSONObject jObj = new JSONObject(response);

                        JSONObject user = jObj.getJSONObject("data");
                        String msgs = user.getString("message");
                        Toast.makeText(getContext(), "" + msgs, Toast.LENGTH_SHORT).show();


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

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



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


            String fname = editTextFname.getText().toString().trim();
            String lname = editTextLname.getText().toString().trim();
            String mnumber = editTextMnumber.getText().toString().trim();
            String email = editTextEmail.getText().toString().trim();
            String pwd = editTextPass.getText().toString().trim();
            String confirmpwd = editTextCpass.getText().toString().trim();

            Map<String, String> params = new HashMap<>();
            params.put("fname", fname);
            params.put("lname", lname);
            params.put("mnumber", mnumber);
            params.put("message", email);
            params.put("name", pwd);
            params.put("name", confirmpwd);

            return params;
        }


    };
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            20000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
    ));
    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    requestQueue.add(stringRequest);


}
 }

这是我的Json Signdata

url: - “... / api / Signup”

{
"code": 2,
"msg": "first name is required\nlast name is required\ncontact phone is required\n",
"details": "",
"request": "[]"
}

这是我带参数的Json Signdata。

url: - “.... / api / Signup?first_name = ram&amp; last_name = verma&amp; email_address=ram@gmail.com& password = 987&amp; cpassword = 987&amp; contact_phone = 2222222”

  {
"code": 2,
"msg": "Sorry but your mobile number is already exist in our records",
"details": "",
"request": "{\"first_name\":\"ram\",\"last_name\":\"verma\",\"email_address\":\"ram@gmail.com\",\"password\":\"987\",\"cpassword\":\"987\",\"contact_phone\":\"2222222\"}"
}

0 个答案:

没有答案