在数据库中获取空白条目

时间:2018-08-12 07:20:10

标签: android json basic-authentication

这是我的代码

public class MainActivity extends AppCompatActivity {

    EditText etFirstname, etLastname, etEmailPhone, etPassword, etTelephone, etConfirm;
    Button btnRegister;
    RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etFirstname = findViewById(R.id.editTextFirstName);
        etLastname = findViewById(R.id.editTextLastName);
        etEmailPhone = findViewById(R.id.editTextEmail);
        etPassword = findViewById(R.id.editTextPassword);
        etTelephone = findViewById(R.id.editTexttelephone);
        etConfirm = findViewById(R.id.editTextConfirmPassword);
        btnRegister = findViewById(R.id.buttonSignUp);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doNormalPostOperation();

            }
        });

    }

    public void doNormalPostOperation() {

        final RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET,
                "https://www.glamorousshe.com/index.php?route=customapi/customer/register", null,
                new Response.Listener<JSONObject>()
                {
                    @Override
                    public void onResponse(JSONObject response) {
                        // display response
                      //  hideProgressDialog();
                        Log.e("login output", queue.toString());
                        Register registerListModel = new GsonBuilder().create().fromJson(queue.toString(), Register.class);


                        Toast.makeText(MainActivity.this, registerListModel.getStatus() + "", Toast.LENGTH_SHORT).show();

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

                        Toast.makeText(getApplicationContext(), "Something went Wrong.. Please Try again..", Toast.LENGTH_SHORT).show();

                    }
                }) {


            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/json");
                String creds = String.format("%s:%s", "glamorousshe07032018", "9426473664");
                String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
                params.put("Authorization", auth);

                params.put("firstname", etFirstname.getText().toString() + "");
                params.put("lastname", etLastname.getText().toString() + "");
                params.put("email", etEmailPhone.getText().toString() + "");
                params.put("password", etPassword.getText().toString() + "");
                params.put("confirm", etConfirm.getText().toString() + "");
                params.put("telephone", etTelephone.getText().toString() + "");
                Log.e("para", params + "");
                return params;

            }
        };




        queue.add(getRequest);

        }
    }

注册模型类

public class Register {

    @SerializedName("data")
    @Expose
    private Data data;
    @SerializedName("status")
    @Expose
    private Integer status;

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

}

获取数据库中的空白字段 在android中单击“注册”按钮时,它会引发错误异常并将空白字段存储在数据库中。

2 个答案:

答案 0 :(得分:0)

服务器可能需要授权,并且您也需要发送令牌

map.put("Authorization", "Bearer token");

答案 1 :(得分:0)

如果我们使用POST而不是GET或GET而不是POST芒,则会发生此错误

因此,在此行将POST更改为GET

StringRequest stringRequest = new StringRequest(Request.Method.GET, "https://www.****.com/api/register",
new Response.Listener<String>()

希望这可能对您有帮助