我制作了GCM application
和local server in PHP
。我想将客户端的注册ID存储到服务器数据库中。我在localhost上运行服务器。我尝试使用IPv4
这样的192.168.x.y/gcm/save.php
地址(它正常运行,并且在我的计算机上运行C:/wamp/www
时识别所有文件) 并将我的本地计算机和Android应用程序连接到same wifi network
,但它不起作用。如何将我的RegId发送到计算机上运行的本地服务器?这是Utils.java
文件:
public class Utils {
static Context context;
public static final String TAG = "Utils";
public static final String UserName = "UserName";
public static final String PROPERTY_REG_ID = "registration_id";
private static final String PROPERTY_APP_VERSION = "appVersion";
public Utils(Context context) {
Utils.context = context;
}
public SharedPreferences getGCMPreferences() {
return context.getSharedPreferences(((ActionBarActivity) context)
.getClass().getSimpleName(), Context.MODE_PRIVATE);
}
public void savePreferences(String key, String value) {
final SharedPreferences prefs = getGCMPreferences();
Log.i(TAG, key + " : " + value);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public String getFromPreferences(String key) {
final SharedPreferences prefs = getGCMPreferences();
String value = prefs.getString(key, "");
if (value.isEmpty()) {
Log.i(TAG, key + " not found.");
return "";
}
return value;
}
String getRegistrationId() {
final SharedPreferences prefs = getGCMPreferences();
String registrationId = prefs.getString(PROPERTY_REG_ID, "");
if (registrationId.isEmpty()) {
Log.i(TAG, "Registration not found.");
return "";
}
// Check if app was updated; if so, it must clear the registration ID
// since the existing regID is not guaranteed to work with the new
// app version.
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION,
Integer.MIN_VALUE);
int currentVersion = getAppVersion();
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}
return registrationId;
}
static int getAppVersion() {
try {
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (NameNotFoundException e) {
// should never happen
throw new RuntimeException("Could not get package name: " + e);
}
}
public void storeRegistrationId(String regId) {
final SharedPreferences prefs = getGCMPreferences();
int appVersion = Utils.getAppVersion();
Log.i(TAG, "Saving regId on app version " + appVersion);
Log.i(TAG, "Reg ID : " + regId);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(PROPERTY_REG_ID, regId);
editor.putInt(PROPERTY_APP_VERSION, appVersion);
editor.commit();
}
public String getCurrentIPAddress() {
//return "http://192.168.1.37/safety_app/save_reg_id.php";
return "http://192.168.14.198/safety_app/save_reg_id.php";
}
public void showToast(final String txt) {
((Activity) context).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, txt, Toast.LENGTH_LONG).show();
}
});
}
}
这是sendRegistrationIdtoBackend
函数:
public void sendRegistrationIdToBackend() {
Log.i(TAG, "sendRegistrationIdToBackend");
Thread thread = new Thread() {
@Override
public void run() {
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost(utils.getCurrentIPAddress());
nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("username",
user_name));
nameValuePairs.add(new BasicNameValuePair("reg_id", regid));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
utils.showToast("reached 6");
HttpResponse response = httpclient.execute(httppost);
String responseBody = EntityUtils.toString(response.getEntity());
utils.showToast("reached 7");
Log.i(TAG, "Response : " + response);
utils.showToast("reached here");
if (responseBody != null) {
utils.showToast("reached nn");
if (responseBody.equalsIgnoreCase("Username already registered")) {
utils.showToast("Username already registered");
hidePB();
} else {
if (responseBody
.equalsIgnoreCase("New Device Registered successfully")) {
utils.savePreferences(Utils.UserName, user_name);
// Persist the regID - no need to register
// again.
utils.storeRegistrationId(regid);
utils.showToast("Device registration successful");
runOnUiThread(new Runnable() {
public void run() {
setPeopleList();
}
});
}
}
}
} catch (Exception e) {
utils.showToast("catch-"+e.getMessage());
//showPB("catch-"+e.getMessage());
Log.d(TAG, "Exception : " + e.getMessage());
}
}
};
thread.start();
}
这是我的save_reg_id.php
:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "safety_app";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$name = $_POST["username"]; // = "VVV";
$reg_id = $_POST["reg_id"]; // = "APA91bEfFaXquuolftF0jy55C5LShdL5Ugu5NrSk_bmwd8wpYW6Ht4dKW5gEq2WFuMFRQD2LOVRF7kN7CtJrh-cTRwXxtELSfUSi4W8L56bi35vYWvylRwZA0qZ02qv9GFYshQENn6Xv-s3yW5sLoaxl-Hka5ZzFuIZBNpiCVQqseG20ckH0e10";
$duplicate_check_sql = "SELECT * FROM registereddevices where username = '$name'";
$suplicate_result = $conn->query($duplicate_check_sql);
if ($suplicate_result->num_rows > 0) {
echo "Username already registered";
}else{
$sql = "INSERT INTO registereddevices (username, reg_id) VALUES ('$name', '$reg_id')";
if (mysqli_query($conn, $sql)) {
echo "New Device Registered successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
?>