0

I have some data that i passed a httpurlconnection to a php script which gets the variables that have been encoded throught the urlencoder, now i want to get a response back from the script i can do this with an echo but that will not solve my problem i want to save data in different variables just like how it is done using the url encoder for the android part and then send those variables back to the android device. How can i accomplish this please. This is the code for sending the data and receiving response

 String data = URLEncoder.encode("studentschool","UTF-8")+"="+URLEncoder.encode(getStudentSchool,"UTF-8")+"&"+
                  URLEncoder.encode("studentdepartment","UTF-8")+"="+URLEncoder.encode(getStudentDepartment,"UTF-8")+"&"+
                  URLEncoder.encode("studentcurrentyear", "UTF-8")+"="+URLEncoder.encode(getStudentCurrentYear, "UTF-8");

            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String response = " ";
            String line = "";
            while ((line = reader.readLine()) != null){
                response+=line;
            }
            reader.close();
            httpURLConnection.disconnect();
            inputStream.close();
            return response;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

And this is my php

require "init.php";

$school = @$_POST["studentschool"];
$department = @$_POST["studentdepartment"];
$school_year = @$_POST["studentcurrentyear"];

$query = "SELECT *
              FROM `users`
              WHERE `school` = '$school'
               AND  `department` = '$department'
               AND  `schoolyear` = '$school_year'
                AND `courserep` = '1' LIMIT 1";

$result= mysqli_query($link,$query);

if (mysqli_num_rows($result)> 0){
  $row = mysqli_fetch_assoc($result);
  $monday = $row["Monday"];
  $tuesday = $row["Tuesday"];
  $wednesday = $row["Wednesday"];
  $thursday = $row["Thursday"];
  $friday = $row["Friday"];

  echo $monday;
  echo $tuesday;
}
10
  • you'd probably be better off encoding the response data using a standard format like JSON or XML which you can easily deserialise in Android, rather than trying to treat the response like some kind of stream. You probably also want to return something (empty object, or error?) in the case where there are no results. Commented Aug 30, 2017 at 14:42
  • P.S. Your code is vulnerable to SQL injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. Commented Aug 30, 2017 at 14:43
  • "will secure it later". IMHO It's much easier to write it correctly the first time, then you don't have to re-test it all later after you make the security changes. Plus it gets you into good habits, and also you don't accidentally forget to do it. Commented Aug 30, 2017 at 14:58
  • "what i am parsing is already json from the android app". 1) No it isn't, what is passed from Android to PHP is a load of data in URLEncoded querystring format. 2) We're not talking about what Android sends to PHP, we're talking about what PHP returns to Android. At the moment PHP just sends a single line of text containing $monday and $tuesday concatenated together, which is un-parseable, because you can't tell which bit is supposed to be which variable. Commented Aug 30, 2017 at 15:00
  • Also as per the example in the answer below (stackoverflow.com/questions/28761558/…) there are much nicer ways to make HTTP requests in android than using low-level stream buffering, both for input and output. You're making this more difficult than it needs to be. Commented Aug 30, 2017 at 15:02

0

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.