Search code examples
phpandroidgetsend

Sending send data through URL parameters in Android


I'm trying to make an app when users fill form, and click send button, to send informations through url parametrs like :

localhost/my.php?param1=Name&param2=SureName

I have read through internet about it, but can't get it, I found some examples but they tell how to pass through Json, I want just to send through link. (I think is easiest way )

My php code is:

<?php

require "connection.php";

if (!isset($_GET['param1'])) {
        echo '<p align="center"> No data passed!</p>"';
    }

if (strcmp($_GET['param1'],'FirstParam')== 0) 
{

$sql= "Query HERE TO ADD INTO DB";

mysqli_query($connection,$sql);

if ($connection->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $connection->error;
}

 mysqli_close($connection);
} 

Any help will be appreciated.


Solution

  • I recommend to have a look on OkHttp. It is an easy library to send HTTP requests in whatever way you like. On Vogella you can also find an example of sending a GET request with additional parameters. Basically it looks like this

    HttpUrl.Builder urlBuilder = HttpUrl.parse("https://api.github.help").newBuilder();
    urlBuilder.addQueryParameter("v", "1.0");
    urlBuilder.addQueryParameter("user", "vogella");
    String url = urlBuilder.build().toString();
    
    Request request = new Request.Builder()
                         .url(url)
                         .build();
    

    And after that you need to send your request (asynchronously) with the help of the OkHttpClient like this

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }
    
        @Override
        public void onResponse(Call call, final Response response) throws IOException {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            } else {
            // do something wih the result
        }
    }
    

    (Code was taken from Vogella)