Volley string request is not working in Kotlin

0

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

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

 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

 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

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"

}

I will be so grateful if u can help me with that :slight_smile: and I f u have any questions just hit me up I’m using volley:1.1.0 compileSdkVersion 29 buildToolsVersion “29.0.3”

I don’t know Volley but looks like you are trying to use POST (isset($_POST['Email']) but is doing the request as GET (Request.Method.GET) and you are setting the parameters as query using getParams().

2 Likes

Hi Eliote thx for the asnwer I changed that but I got now and error Called “android.volley.timeoutError”
do u have any idea what could be the reasin for this error ?

Are you sure the endpoint is reachable? the URL you posted is using https, is that correct?
In production, sure, https is necessary. But for internal LAN development this is really a pain to setup.

2 Likes

Heyy Eliote thx for your time how can I check if the endpoint is reachable ?

Try to open it in a browser, or put some breakpoints e the webservice to see if it’s called, or ping the server, or create a test url…

1 Like

Yeah I tried that with postman and in the browser too it works but I think the problem is in my endpoint in kotlin, but I don’t know, I think he is not sending the request

I mean from the android device try to reach the server, not from the same pc the server is running.

1 Like

Ahh I got u ok I will try it and and I will let u know

Hi Eliote u have right, I tried that on my browser in myphone and it is not working !!
What is the reason for that ?

1 Like

Hello, your server is probably only listening to localhost. Try reading the documentation of the server you are using on how to configure it. A quick google search for “MyServerName external access” must do the trick too. :wink:

1 Like

Hey Eliote, it works now again I just had to change the endpoint from localhost to the Wlan Ip address
thx man for your time I appreciate that

2 Likes

Hallo . how are you , actually I have the same problem here , I am trying to upload an Image to server using kotlin .

can you guys help me with it thanks

try adding “/” at the end of the URL
I remember volley request not working properly (like POST/DELETE being converted to GET) with or without the slash at the end (with or without I forgot lol)