凌空中意外的代码400错误

时间:2016-07-27 12:14:53

标签: android android-volley

我正在尝试注册客户已注册但收到错误回复。在成功和失败的情况下,仅获得错误响应并在我的日志中出现此错误" BasicNetwork.performRequest:意外的响应代码400"。

这是我的代码

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class CustomerRegistration extends BaseActivity implements View.OnClickListener {
        FloatingActionButton registrationbtn;
        EditText firstname,lastname,loginid,confirmlogin,mobile,city,password,confirmpassword;
        AutoCompleteTextView country;
        String URL_Register="http://188.166.237.82/api/customer";
        String URL_Country="http://188.166.237.82/api/country";
        ArrayList<String>arrayList;
        RequestQueue requestQueue;
        ArrayAdapter<String>arrayAdapter;
        String firstName,lastName,loginId,confirmLoginId,Mobile,City,Password,confirmPassword,Country;
        String namepattern="^[a-zA-Z\\s]*$";
        String emailpattern="[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
        String mobilepattern="[0-9]{10}$";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_registration);
        registrationbtn= (FloatingActionButton) findViewById(R.id.cust_btn_registeration);
        country= (AutoCompleteTextView) findViewById(R.id.cust_act_country);
        firstname= (EditText) findViewById(R.id.cust_et_firstname);
        lastname= (EditText) findViewById(R.id.cust_et_lastname);
        loginid= (EditText) findViewById(R.id.cust_et_loginid);
        confirmlogin= (EditText) findViewById(R.id.cust_et_confirm_loginid);
        mobile= (EditText) findViewById(R.id.cust_et_mobile);
        city= (EditText) findViewById(R.id.cust_et_city);
        password= (EditText) findViewById(R.id.cust_et_password);
        confirmpassword= (EditText) findViewById(R.id.cust_et_confirm_password);
        arrayList=new ArrayList<String>();
        clientSideValidations();
        registrationbtn.setOnClickListener(this);
        country.setOnClickListener(this);
        getCountryList();
    }
    private void  getCountryList(){
        JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(URL_Country, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                for (int i=0;i<response.length();i++){
                    try {
                        JSONObject jsonObject=response.getJSONObject(i);
                        String countryname=jsonObject.getString("name");
                        arrayList.add(countryname);
                        arrayAdapter=new ArrayAdapter<String>(CustomerRegistration.this,android.R.layout.simple_spinner_item,arrayList);
                        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        country.setAdapter(arrayAdapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

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

            }
        });
        requestQueue=Volley.newRequestQueue(this);
        requestQueue.add(jsonArrayRequest);
    }

    @Override
        public void onClick(View v) {
        firstName=firstname.getText().toString().trim();
        lastName=lastname.getText().toString().trim();
        loginId=loginid.getText().toString().trim();
        confirmPassword=confirmpassword.getText().toString().trim();
        confirmLoginId=confirmlogin.getText().toString().trim();
        Password=password.getText().toString().trim();
        Mobile=mobile.getText().toString().trim();
        City=city.getText().toString().trim();
        Country=country.getText().toString().trim();
       if( firstName.isEmpty()||lastName.isEmpty()||loginId.isEmpty()||confirmLoginId.isEmpty()||Mobile.isEmpty()||City.isEmpty()
        ||Password.isEmpty()||confirmPassword.isEmpty()||Country.isEmpty()){
           Toast.makeText(CustomerRegistration.this, "Some Fields Are Empty", Toast.LENGTH_SHORT).show();
        }else {

           sendFields();
       }
    }
    private void sendFields(){
        progressDialog.setMessage("Loading......");
        progressDialog.show();
        StringRequest stringRequest=new StringRequest(Request.Method.POST, URL_Register, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressDialog.hide();
                try {
                    JSONObject jsonObject1=new JSONObject(response);
                    String result=jsonObject1.getString("message");
                    Toast.makeText(CustomerRegistration.this, result, Toast.LENGTH_SHORT).show();
//                    AlertDialog.Builder builder=new AlertDialog.Builder(CustomerRegistration.this).setTitle("Congratulations.....!"+result)
//                            .setMessage("Successfully Registered..! Activation Link Send to your Registered Mail Id ").setNeutralButton("OK", new DialogInterface.OnClickListener() {
//                                @Override
//                                public void onClick(DialogInterface dialog, int which) {
//                                    startActivity(new Intent(CustomerRegistration.this,LoginActivity.class));
//                                }
//                            });
//                    Dialog dialog=builder.create();
//                    dialog.setCanceledOnTouchOutside(false);
//                    dialog.show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.hide();
                NetworkResponse networkResponseVolley = error.networkResponse;
                String error_message = new String(networkResponseVolley.data);
                try {
                    JSONObject jsonObject = new JSONObject(error_message);
//                    String bookingError=jsonObject.getString("message");
//                    Toast.makeText(CustomerRegistration.this, bookingError, Toast.LENGTH_SHORT).show();
                    JSONArray jsonArray = jsonObject.getJSONArray("errors");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        String errorResponse = jsonArray.getString(i);
                        Toast.makeText(CustomerRegistration.this, errorResponse.toString(), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String,String> params=new HashMap<String, String>();
                params.put("company","GeoAppstractSolutions");
                params.put("firstname",firstname.getText().toString().trim());
                params.put("lastname",lastname.getText().toString().trim());
                params.put("phone",mobile.getText().toString().trim());
                params.put("loginid",loginid.getText().toString().trim());
                params.put("city",city.getText().toString().trim());
                params.put("country",country.getText().toString().trim());
                params.put("password",password.getText().toString().trim());
                params.put("password_confirmation",confirmpassword.getText().toString().trim());
//                params.put("Content-Type", "application/json");
                return params;

            }
        };
        requestQueue= Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

    }
    private void clientSideValidations(){
        firstname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                firstName=firstname.getText().toString().trim();
                if(!firstName.matches(namepattern)){
                    firstname.setError("Use Alphabects only");
                }else{
                    firstname.setError(null);
                }
            }
        });
        lastname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                lastName=lastname.getText().toString().trim();
                if(!lastName.matches(namepattern)){
                    lastname.setError("Use Alphabects only");
                }else{
                    lastname.setError(null);
                }
            }
        });
        mobile.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Mobile=mobile.getText().toString().trim();
                if(!Mobile.matches(mobilepattern)){
                    mobile.setError("Mobile Number length should be 10");
                }else{
                    mobile.setError(null);
                }
            }
        });
        loginid.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                loginId=loginid.getText().toString().trim();

                if(!loginId.matches(emailpattern)){
                    loginid.setError("Invalid E-mail Format");
                }else{
                    loginid.setError(null);
                }
            }
        });
        confirmlogin.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                confirmLoginId=confirmlogin.getText().toString().trim();

                if(!confirmLoginId.equals(loginId)){
                    confirmlogin.setError("Both Login and ConfirmLogin should be match");
                }else{
                    confirmlogin.setError(null);
                }
            }
        });

        city.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                City=city.getText().toString().trim();
                if(!City.matches(namepattern)){
                    city.setError("Use Alphabects only");
                }else{
                    city.setError(null);
                }
            }
        });
        country.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Country=country.getText().toString().trim();
                if(!Country.matches(namepattern)){
                    country.setError("Use Alphabects only");
                }else{
                    country.setError(null);
                }
            }
        });
        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Password=password.getText().toString().trim();

                if (Password.length()<6){
                    password.setError("Password should be more than 6 characters");
                }else{
                    password.setError(null);
                }
            }
        });
        confirmpassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                confirmPassword=confirmpassword.getText().toString().trim();
                if (!confirmPassword.equals(Password)){
                    confirmpassword.setError("Both Possword and ConfirmPassword should be match");
                }else{
                    confirmpassword.setError(null);
                }
            }
        });
    }


}

1 个答案:

答案 0 :(得分:0)

尝试使用postman中的相同参数测试您的webservice并检查状态代码。如果它是相同的,那么你没有发送正确的参数。