Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a response from PHP to an Android/Java mobile app?

Tags:

java

android

php

I currently have a piece of code in my Android application that picks up the devices IMEI and sends that IMEI as a parameter to a PHP script that is hosted on the Internet.

The PHP script then takes the IMEI parameter and checks a file to see if the IMEI exists in the file, if it does I want to be able to let my Android application know that the IMEI exists. So essentially I just want to be able to return True to my application.

Is this possible using PHP?

Here is my code so far:

Android/Java

//Test HTTP Get for PHP

        public void executeHttpGet() throws Exception {
            BufferedReader in = null;
            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI("http://testsite.com/" +
                        "imei_script.php?imei=" + telManager.getDeviceId()
                        ));
                HttpResponse response = client.execute(request);
                in = new BufferedReader
                (new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();
                String page = sb.toString();
                System.out.println(page);
                } finally {
                if (in != null) {
                    try {
                        in.close();
                        } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

The above sends the IMEI as a parameter to the PHP script which picks it up successfully and runs a check against the file successfully, however I neeed to then be able to send a positive response back from the PHP script if the IMEI matches one in the file.

Here is the PHP:

<?php
    // to return plain text
    header("Content-Type: plain/text"); 
    $imei = $_GET["imei"];

    $file=fopen("imei.txt","r") or exit("Unable to open file!");

    while(!feof($file))
     {
    if ($imei==chop(fgets($file)))
     echo "True";
     }

    fclose($file);

?>

So instead of echo True I want to be able to let my application know that the IMEI was found, is this possible and if so what should I be using to achieve it?

like image 566
Donal Rafferty Avatar asked Jan 07 '11 10:01

Donal Rafferty


2 Answers

this is good stuff! actually, you're nearly there. your php shouldn't change, your java should! you just need to check the result of the response inside your java code. redeclare your java method as

public String executeHttpGet() {

then, let this method return the variable page.

now you can create a helper method somewhere. if you put it in the same class as executeHttpGet, it will look like this:

public boolean imeiIsKnown(){
    return executeHttpGet().equals("True");
}

now you can call this method to find out if your imei is known in your php backend.

like image 175
davogotland Avatar answered Sep 21 '22 12:09

davogotland


I'm not sure is it good for you or not - but you can use headers. If the IMEI was found you can send header("Status: HTTP/1.1 200 OK") otherwise send header("Status: 404 Not Found").

And then you should check response status in your application.

like image 25
Igor Avatar answered Sep 20 '22 12:09

Igor