Mohammad ZF
Mohammad ZF

Reputation: 123

volley post request issue

I'm not an Android / Java developer but I need to fix this problem in an open source project.

I encountered the error "E / Volley: [300] BasicNetwork.performRequest" while submitting the request and no further explanation was given for this error.

btnSubmit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String URL ="http://someURL/api/crm/check_login";
        Response.Listener<String> listener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                // LOG Response Or ... 
            }
        };           
        StringRequest request = new StringRequest(Request.Method.POST, URL, listener, errorListener)
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {    
                Map<String, String> params = new HashMap<>();
                params.put("username","TEST");
                params.put("password","123456");
                return  params;    
            }
        };                        
        AppController.getInstance().addToRequestQueue(request);    
    });
}

Upvotes: 0

Views: 67

Answers (2)

Rajasekaran M
Rajasekaran M

Reputation: 2538

You can use your url directly http://192.168.0.130:1010/api/CRM/check_login?username=test&password=123

Because getParams() method is used for application/form-data not for Query params so it's would not work for you.

Source :

btn_volley.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String username="test";//you can get username and password from edittexts
        String password="123";

        String URL = "http://192.168.0.130:1010/api/CRM/check_login?username="+username+"&password="+password;

        Response.Listener<String> listener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response.isEmpty()) {
                    Toast.makeText(getApplicationContext(), "test",Toast.LENGTH_LONG).show();
                } else {
                       Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show();
                }
            }
        };

        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                       Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();
            }
        };

        StringRequest request = new StringRequest(Request.Method.POST, URL, listener, errorListener);

        AppController.getInstance().addToRequestQueue(request);

    }
});

Upvotes: 1

Xay
Xay

Reputation: 248

You need to add Content-Type to the header.

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json; charset=utf-8");
    return headers;
}

Upvotes: 0

Related Questions