1

I have a database with football players from FIFA and I'm able to search it from my Android application based on their names. I want to be able to search it based on the players rating as well, but I'm not able to do that.

This is how I try to send the two criteria I have (player name and rating) from my application and this is where it doesn't work. It works if I just have one Uri.builder() with the searchQuery variable which is the name of the player. But when I try to add another Uri.builder() with the variable minRating it can't make the connection to the database.

  // Setup HttpURLConnection class to send and receive data from PHP and MySQL
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");

            // setDoInput and setDoOutput to true as we send and recieve data
            conn.setDoInput(true);
            conn.setDoOutput(true);

            // add parameter to our above url
            Uri.Builder builder = new Uri.Builder().appendQueryParameter("searchQuery", searchQuery);
            String query = builder.build().getEncodedQuery();

            Uri.Builder builder2 = new Uri.Builder().appendQueryParameter("minRating", minRating);
            String minRating = builder2.build().getEncodedQuery();

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(query);
            writer.write(minRating);
            writer.flush();
            writer.close();
            os.close();
            conn.connect();

This is how I receive the data with my PHP-file and query the database:

<?php
 include 'config.inc.php';

 if(isset($_POST['searchQuery']))
 {
      $search_query=$_POST['searchQuery'];
      $min_rating=$_POST['minRating'];

      $sql = "SELECT Name, Club_Position, Rating from spillere where Name LIKE '%$search_query%'
              AND Rating > $min_rating";

      $statement = $conn->prepare($sql);
  $statement->bindParam(':search_query',$search_query, PDO::PARAM_STR);
      $statement->execute();
      if($statement->rowCount())
      {
    $row_all = $statement->fetchall(PDO::FETCH_ASSOC);
    header('Content-type: application/json');
    echo json_encode($row_all);
      }  
      elseif(!$statement->rowCount())
      {
    echo "no rows";
      }
 }    
 ?>

This is what my stack trace looks like:

Image of stacktrace

How can I take two or more search criteria from my Android application, send it to the PHP-file and query the database?

2
  • but you already send two parameters searchQuery and minRating. You can do the same to send further parameters. Or what exactly do you want? Commented Aug 9, 2017 at 9:28
  • 1
    @Edwin Sorry for not making it clear. I can't send both of the parameters I can only send (searchQuery) . When I try to send (minRating) it doesn't work. So there must be something wrong with the way that I use the (Uri.builder). Commented Aug 9, 2017 at 9:33

1 Answer 1

1
 Uri.Builder builder = new Uri.Builder().appendQueryParameter("searchQuery", searchQuery);
 String query = builder.build().getEncodedQuery();

 Uri.Builder builder2 = new Uri.Builder().appendQueryParameter("minRating", minRating);
 String minRating = builder2.build().getEncodedQuery();

Combine both above parameter in single Query Parameter:

Uri.Builder builder = new Uri.Builder()
             .appendQueryParameter("searchQuery", searchQuery)
             .appendQueryParameter("minRating", minRating);
String query = builder.build().getEncodedQuery();

//Now write query param to OutputStream 
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Simple solution to an annoying problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.