Click here to Skip to main content
15,851,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys I'm stuck with this problem for a couple of weeks now, I hope u can help me with that.
So I have a local server with database(PHPMyadmin + MySQL), I have a request that I call as follows http://localhost/WebApi/v1/?op=addBars with Postman  I pass 4 values with this request first_name, Last_name, Email and Pass and they will be saved in then database  and it works fine. 
but when I try to send data to database with Volley in Kotlin: I don;t get any answer and no error as well 
so this how my V1.php looks like 




PHP
case 'addBars':
				if(isset($_POST['Email']) && isset($_POST['Pass']) && isset($_POST['First_Name'])&& isset($_POST['Last_Name'])){
					$db = new DbOperation(); 
					if($db->createBars($_POST['Email'], $_POST['Pass'],$_POST['First_Name'], $_POST['Last_Name'])){
						$response['error'] = false;
						$response['message'] = 'Bars added successfully';
					}else{
						$response['error'] = true;
						$response['message'] = 'Could not add Bars';
			}                  
				}else{
					$response['error'] = true; 
					$response['message'] = 'Required Parameters are missing';
				}
			break;

this also how the create bars looks like in DbOperation.php



PHP
public function createBars($Email,$Pass,$First_Name,$Last_Name){
		$stmt = $this->con->prepare("INSERT INTO Bars (Email, Pass,First_Name,Last_Name) VALUES (?,?,?,?)");
		$stmt->bind_param("ssss", $Email, $Pass,$First_Name,$Last_Name);
		if($stmt->execute())
			return true; 
		return false; 
	}


This is also how I use the Volley request in Kotlin


Kotlin
val stringRequest = object : StringRequest(Request.Method.POST, EndPoints.URL_ADD_Bars,
                Response.Listener { response ->
                    try {
                        
                        val obj = (response)
                        Toast.makeText(applicationContext ,"test "+obj+"" , Toast.LENGTH_SHORT).show()

                       // Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_LONG).show()
                    } catch (e: JSONException) {
                        Toast.makeText(applicationContext, "response,"+response.toString()+"", Toast.LENGTH_LONG).show()
                        e.printStackTrace()
                    }
                },

                object : Response.ErrorListener {
                    override fun onErrorResponse(volleyError: VolleyError) {
                         Toast.makeText(applicationContext, "error,"+volleyError.toString()+"", Toast.LENGTH_LONG).show()

                    }
                })
            
            {
                @Throws(AuthFailureError::class)
                override fun getParams(): Map<String, String> {
                    val params = HashMap<String, String>()
                    params.put("Email",Email)
                    params.put("Pass",Pass)
                    params.put("First_Name",First_Name)
                    params.put("Last_Name",Last_Name)
                    return params
                }
        }

        //adding request to queue
        VolleySingleton.instance?.addToRequestQueue(stringRequest)}

and that is how my EndPoints looks like

Kotlin
object EndPoints {
    private val URL_ROOT = "https://192.130.180.209/WebApi/v1/?op="
    val URL_ADD_Bars = URL_ROOT + "addBars"
    val URL_GET_Bars = URL_ROOT + "getBars"
    val URL_Bars_info = URL_ROOT + "Bars_info"

    val URL_Bars_Listing = URL_ROOT + "Bars_Listing"

    val URL_Bars_Listing_name_adress = URL_ROOT + "Bars_Listing_name_adress"
    val URL_Bars_info_update_api = URL_ROOT + "Bars_info_update_api"

}




What I have tried:

I will be so grateful if u can help me with that :) and I f u have any questions just hit me up
I'm using  volley:1.1.0
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900