1

I want send parameters by volley to php page in host. But I get the following error.

BasicNetwork.performRequest: Unexpected response code 400 

This is my php code

<?php if ($_SERVER['REQUEST_METHOD']=='POST'){ $user = $_POST['email']; $pass = $_POST['password']; $sql = "select * from users where email = '$user' AND password = '$pass'"; require_once('config.php'); $result = mysqli_query($con , $sql); $check = mysqli_fetch_array($result); if(isset($check)){ echo "success"; }else{ echo "failure"; } mysqli_close($con); } ?> 

This is file config.php

<?php $serverName = "localhost"; $userName = "root"; $password = ""; $dbName = "testDb" $con = mysqli_connect($serverName , $userName , $password , $dbName) or die("connect failed"); ?> 

and This is my android code

@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); editTextEmail = (EditText) findViewById(R.id.editTextEmail); editTextPassword = (EditText) findViewById(R.id.editTextPassword); buttonLogin = (Button) findViewById(R.id.buttonLogin); buttonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login(); } }); } @Override protected void onResume() { super.onResume(); SharedPreferences sharedPreferences = getSharedPreferences(config.SHARED_PREF_NAME , Context.MODE_PRIVATE); loggedIn = sharedPreferences.getBoolean(config.LOGGEDIN_SHARED_PREF , false); Log.e("txt" , "text is : " + loggedIn); if (loggedIn){ Intent intent = new Intent(loginActivity.this , profileActivity.class); startActivity(intent); } } private void login(){ final String email = editTextEmail.getText().toString().trim(); final String password = editTextPassword.getText().toString().trim(); StringRequest stringRequest = new StringRequest(Request.Method.POST, config.LOGIN_URL, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.i("StartActivity", response.toString()); if(response.equalsIgnoreCase(config.LOGIN_SUCCESS)){ //Creating a shared preference SharedPreferences sharedPreferences = loginActivity.this.getSharedPreferences(config.SHARED_PREF_NAME, Context.MODE_PRIVATE); //Creating editor to store values to shared preferences SharedPreferences.Editor editor = sharedPreferences.edit(); //Adding values to editor editor.putBoolean(config.LOGGEDIN_SHARED_PREF, true); editor.putString(config.EMAIL_SHARED_PREF, email); //Saving values to editor editor.commit(); //Starting profile activity Intent intent = new Intent(loginActivity.this, profileActivity.class); startActivity(intent); }else{ //If the server response is not success //Displaying an error message on toast Toast.makeText(loginActivity.this, "Invalid username or password", Toast.LENGTH_LONG).show(); Log.e("s" , "invalid sssssssssssssssssssssss"); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }){ @Override public Map<String, String> getParams() { Map<String,String> params = new HashMap<>(); params.put(config.KEY_EMAIL, email); params.put(config.KEY_PASSWORD, password); return params; } }; RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(stringRequest); } 

And when that runs onResponse else command is executed . The transmission parameters with post method values ​​are wrong

thankyou

2 Answers 2

1

BasicNetwork.performRequest: Unexpected response code 400

A 400 means that the request was malformed. In other words, the data stream sent by the client to the server didn't follow the rules.

Error Caused by:

You forgot to put semicolon after the $dbName, which causes the db to die again and again without showing any Exception via Login Link, Correct:

$dbName = "testDb"; //<--here forgot to add semicolon 

Correction - Try code:

@Override public Map<String, String> getParams() { Map<String,String> params = new HashMap<>(); params.put("user", email); //here params.put("pass", password); //here return params; } }; 

Correction - Require once before interacting with table:

<?php $user = $_POST['email']; $pass = $_POST['password']; require_once('config.php'); //here $sql = "select * from users where email = '$user' AND password = '$pass'"; 
Sign up to request clarification or add additional context in comments.

4 Comments

Okay, now I'm testing
BasicNetwork.performRequest: Unexpected response code 400 for librarylib.gigfa.com/login.php
It must be submitted using the method of post parameters to success message is printed
I think i found the answer, Did any other operations are working on your db ?
0

Instead of overriding getParams() try overriding getBodyContentType() and getBody().

Something like this :

StringRequest stringRequest = new StringRequest(Request.Method.POST, config.LOGIN_URL, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.i("StartActivity", response.toString()); if(response.equalsIgnoreCase(config.LOGIN_SUCCESS)){ //Creating a shared preference SharedPreferences sharedPreferences = loginActivity.this.getSharedPreferences(config.SHARED_PREF_NAME, Context.MODE_PRIVATE); //Creating editor to store values to shared preferences SharedPreferences.Editor editor = sharedPreferences.edit(); //Adding values to editor editor.putBoolean(config.LOGGEDIN_SHARED_PREF, true); editor.putString(config.EMAIL_SHARED_PREF, email); //Saving values to editor editor.commit(); //Starting profile activity Intent intent = new Intent(loginActivity.this, profileActivity.class); startActivity(intent); }else{ //If the server response is not success //Displaying an error message on toast Toast.makeText(loginActivity.this, "Invalid username or password", Toast.LENGTH_LONG).show(); Log.e("s" , "invalid sssssssssssssssssssssss"); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }){ @Override public byte[] getBody() throws AuthFailureError { final JSONObject body = new JSONObject(); try { body.put(config.KEY_EMAIL, email); body.put(config.KEY_PASSWORD, password); } catch (JSONException e) { e.printStackTrace(); } return body.toString().getBytes(); } @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = new HashMap<>(); headers.put("Content-Type","application/json"); return headers; } }; 

Check this question : StringRequest with JSON body

7 Comments

The transmitted data is not the type of json
@Ali then of what type?
The transmitted data are of type string
@Ali Did you try with the above mentioned code? Looking at your params suggests that you shouldn't have any issue with content type being application/json
I have updated the getBody method to send your params.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.