your code basically sounds, all you have to do is tweak it a bit. I mixed and matched the answers above because I needed to accomplish exactly what you were trying to do. I created a database instead of checking txt files.
CREATE TABLE IF NOT EXISTS `user_device` ( `Id_User_Device` int(11) NOT NULL auto_increment, `Nr_User_Device` varchar(60) collate utf8_bin NOT NULL, `Ic_User_Device_Satus` int(11) NOT NULL default '1', PRIMARY KEY (`Id_User_Device`), KEY `Nr_User_Device` (`Nr_User_Device`,`Ic_User_Device_Satus`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=20 ;
android java code would be (don't forget to create the correct settings in the main.xml layout file by inserting 2 elements into the helloworld classic screen:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URI; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class ZdeltestEMEIActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DeviceUuidFactory deviceUuidFactory = new DeviceUuidFactory(this); String deviceUuid = deviceUuidFactory.getDeviceUuid().toString(); Log.d("tgpost",deviceUuid); try { String webPostAnswer = deviceIdCheck(deviceUuid); if (webPostAnswer != null) { TextView tv1 = (TextView) findViewById(R.id.textdisplay01); TextView tv2 = (TextView) findViewById(R.id.textdisplay02); tv1.setText(webPostAnswer); tv2.setText(deviceUuid); Log.d("tgpost", "okok "+webPostAnswer); } else { Log.d("tgpost", "nono empty"); } } catch (Exception e) {
with extra class
import android.content.Context; import android.content.SharedPreferences; import android.provider.Settings.Secure; import android.telephony.TelephonyManager; import java.io.UnsupportedEncodingException; import java.util.UUID; public class DeviceUuidFactory { protected static final String PREFS_FILE = "device_id.xml"; protected static final String PREFS_DEVICE_ID = "device_id"; protected static UUID uuid; public DeviceUuidFactory(Context context) { if( uuid ==null ) { synchronized (DeviceUuidFactory.class) { if( uuid == null) { final SharedPreferences prefs = context.getSharedPreferences( PREFS_FILE, 0); final String id = prefs.getString(PREFS_DEVICE_ID, null ); if (id != null) {
on the php side:
<?php // to return plain text // header("Content-Type: plain/text"); include('/home/public_html/ConnStrDB.php'); $deviceId = $_GET["deviceId"]; $sql = "SELECT Nr_User_Device FROM user_device WHERE Nr_User_Device = '".$deviceId."'"; $result = mysql_query($sql); if ($result) { $row = mysql_fetch_array($result); if ($row[0]) {$deviceIdFile = $row[0];} else {$deviceIdFile = "device not found";} } else { $deviceIdFile = "no check was made, empty set"; } echo $_GET["deviceId"]." ".$deviceIdFile; ?>
and (so you don't have to insert numbers manually (just change the php file name in submit):
<?php // to return plain text // header("Content-Type: plain/text"); include('/home/public_html/ConnStrDB.php'); $deviceId = $_GET["deviceId"]; $sql = "SELECT Nr_User_Device, Ic_User_Device_Status FROM user_device WHERE Nr_User_Device = ".$deviceId; $sql = "INSERT INTO user_device (Nr_User_Device) VALUES ('".$deviceId."')"; $result = mysql_query($sql); if ($result) { $deviceIdFile = "device inserted"; } else { $deviceIdFile = "not inserted"; } echo $_GET["deviceId"]." ".$deviceIdFile; ?>
if imei is displayed 3 times on your mobile screen (the one that was received in php and the one that was received in the database).
ConnStrDB.php is a file that contains your full connection to the MySQL database.
if you answer with long text, the android application will receive it, as well as a detailed version of any php warning. if you don't need json, you can respond to any xml via php echo. Thanks for your question, very helpful! and thanks for the GREAT answers!