Android:将OkHttp与PHP和MySQL结合使用
我正在Android中构建一个用户注册模块。下面是RegistrationManager.java文件,它通过向服务器发送POST请求来处理数据库中的用户注册Android:将OkHttp与PHP和MySQL结合使用,php,android,mysql,okhttp,Php,Android,Mysql,Okhttp,我正在Android中构建一个用户注册模块。下面是RegistrationManager.java文件,它通过向服务器发送POST请求来处理数据库中的用户注册 import com.marshall.opensurvey.sqlite.user.User; import com.squareup.okhttp.Call; import com.squareup.okhttp.Callback; import com.squareup.okhttp.FormEncodingBuilder; imp
import com.marshall.opensurvey.sqlite.user.User;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;
// user controller class specified in registration activities.
public class RegistrationManager {
private OkHttpClient client = new OkHttpClient();
private final String SERVER_URL = ServerURL.URL_REGISTER;
public static boolean error;
// register a new user into mysql database
public void registerUserIntoMySQL(final User newUser) {
RequestBody body = new FormEncodingBuilder()
.add("tag", "register")
.add("email", newUser.getEmail())
.add("name", newUser.getName())
.add("password", newUser.getPassword())
.build();
Request request = new Request.Builder().url(SERVER_URL).post(body).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
}
});
}
}
因此,当服务器收到名为register的标记时,服务器应该在数据库中注册新用户。这是在Register.php文件中定义的
<?php
if(isset($_POST['tag']) && $_POST['tag'] != '') {
$tag = $_POST('tag');
require_once 'mysql/DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if($tag == 'register') {
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) {
// receiving the post params
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// check if user is already existed with the same email
if ($db->userExists($email)) {
// user already exists
$response["error"] = TRUE;
$response["error_msg"] = "User already exists: " . $email;
echo json_encode($response);
} else {
// create a new user.
$user = $db->storeUser($name, $email, $password);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (name, email or password) is missing!";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Inapproriate tag.";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required tag is missing!";
echo json_encode($response);
}
?>
这些是我到目前为止编写的代码,但是我发现MySQL数据库是空的。有什么解决方案吗?那么,这些php变量设置了吗?告诉我们代码是如何流动的。您得到的答复是什么?您没有实现onFailure和onResponse。那么如何通知您呢?您是否在AndroidManifest.xml中设置了INTERNET权限?是否有logcat可用?试着把日志放进去。我。。。内部onResponse和onFailure