-2

I have found lots of responses about it but all of them are written with deprecated functions or don't work in my case.

I need to get the IMEI number (unic ID of a device) of the mobile device where my app will be installed, and then to send that value (in form of a string) to a php file located in a folder I created in my website (because the app has a webview where I visualize that website and I want to save the IMEI into a data base).

So far, I got this in my code:

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.chapatelo.hol.es/php/postIMEI.php");

    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("IMEI", telephonyManager.getDeviceId()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

and I have <uses-permission android:name="android.permission.INTERNET"/> and also <uses-permission android:name="android.permission.READ_PHONE_STATE"/> in the manifest.

And this is the php file:

<?php

require_once("conexion.php");

if(!isset($_SESSION)){
    session_start();
}

$imei = $_POST["IMEI"];

$query1 = "SELECT * FROM usuarios WHERE imei = '$imei'";

if($resp1 = mysqli_query($conexion, $query1)){

    if(mysqli_num_rows($resp1) == 0){

        $query2 = "INSERT INTO usuarios (imei) VALUES ('$imei')";

        if(mysqli_query($conexion, $query2)){
            $_SESSION["imei"] = $imei;
            echo true;
        }else{
            echo "error de conexión";
        }

    }else{
        echo "Ya existe el imei en la db";
    }
}else{
    echo "error de conexión";
}

mysqli_close($conexion);

?>

However, and not mentioning that almost every function such as httpclient is deprecated, nothing of that works.

I gave permitions 755 (read and execute) to the php folder and to the file I access through my app, but nothing happens...

Is there any "new version" of those functions I'm using to get the imei and then send it to the php file?

Is because of the deprecated functions that my app doesn't save the imei in the data base?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Use the log and see whether you are getting the IMEI number, and whether the said HTTP methods are actually working properly. Use error_log on the backend also. Check whether the fault is on the Android side or PHP side so that you can narrow down what is going wrong! Currently with this information, I don't think help is near. – Narayan Acharya Jul 21 '16 at 19:31
  • Not every android device has an IMEI. I believe CDMA doesn't use it (or is it GSM? One of the two) and tablets without cellular data won't. As for it not saving- did you get a hit on the web server in your logs? Try and figure out what part is breaking. – Gabe Sechan Jul 21 '16 at 19:31
  • @Narayan Acharya I don't know much about android or java. I'm new in the android "world". Could you please tell me how to implement those error handlers into my code? – Cristian Annese Jul 21 '16 at 19:35
  • You need to request a run time permission for reading the phone state as it's considered a dangerous permission. https://developer.android.com/training/permissions/requesting.html – Pztar Jul 21 '16 at 19:36
  • hmm As far as I know, every device has an unic ID called imei. But after what you said, I'm not totally sure... I don't get any hit from the server side and I don't know how to figure out if the problem is in the code or in the server side. I tested the php file and conection with my website (from a pc) and sending via ajax a false imei to that file so that I could find out if everything worked well. And it did so... – Cristian Annese Jul 21 '16 at 19:37
  • @Pztar Do you think the only problem would be that permission? – Cristian Annese Jul 21 '16 at 19:58
  • @CristianAnnese That would be the problem if the device you're testing on is Android 6.0+. Are you able to get the `IMEI` for sure? Check to see if you get it before you do any actual sending. – Pztar Jul 21 '16 at 20:27
  • @Pztar So, if I install the app in device running api < 6.0 the permition I used is enough? – Cristian Annese Jul 21 '16 at 20:29
  • Then you don't need the run time permission, in that case check to make sure you are getting the `IMEI` add `Log.i("IMEI", telephonyManager.getDeviceId())` right before you call your `try` block and check your output to see if you are getting a value. – Pztar Jul 21 '16 at 20:32
  • @Pztar Thank you for your help. Could you please post it into a response to this question to let me see well how to implement what you've just written? I don't underatand much about android... – Cristian Annese Jul 21 '16 at 20:35
  • I wrote `TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); String device_id = telephonyManager.getDeviceId(); Toast.makeText(MainActivity.this, "IMEI: " + device_id, Toast.LENGTH_SHORT).show();` It should work as well right? – Cristian Annese Jul 21 '16 at 20:40

1 Answers1

0

You can refer this, might help you

java

 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();
                            }
                        }
                    }
                }

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);

?>
Priyanka
  • 67
  • 1
  • 6
  • Thank you for your response. I don't understand why you wrote a code to create a file with the imei into it as plain text in the php file... I need to save it into a server, not into a file containing a list of imeis. Anyways, I will test the java code. It seems to be helpfull ! I'll tell you whether it worked or not. – Cristian Annese Jul 21 '16 at 19:41