简体   繁体   English

获取移动设备的 IMEI 并通过 POST 将其发送到 php 文件

[英]Get IMEI of a mobile device and send it via POST to a php file

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).我需要获取将安装我的应用程序的移动设备的 IMEI 号码(设备的 unic ID),然后将该值(以字符串的形式)发送到位于我在我的文件夹中创建的文件夹中的 php 文件网站(因为该应用程序有一个 web 视图,我可以在其中可视化该网站,并且我想将 IMEI 保存到数据库中)。

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.我在清单中有<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.READ_PHONE_STATE"/>

And this is the php file:这是 php 文件:

<?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.但是,并没有提到几乎所有功能(例如httpclient弃用),这些都不起作用。

I gave permitions 755 (read and execute) to the php folder and to the file I access through my app, but nothing happens...我给了 php 文件夹和我通过我的应用程序访问的文件的许可 755(读取和执行),但没有任何反应......

Is there any "new version" of those functions I'm using to get the imei and then send it to the php file?我用来获取imei然后将其发送到php文件的那些函数是否有任何“新版本”?

Is because of the deprecated functions that my app doesn't save the imei in the data base?是因为我的应用程序不将imei 保存在数据库中的弃用功能吗?

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

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

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM