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