java让Android应用程序与PHP文件通信
我正在尝试让我的安卓应用程序与我的朋友PHP文件通信,该文件将数据存储到我们的数据库中,然后将数据注册到新的用户帐户。我不熟悉PHP,但我能阅读它。但无论我怎么做,似乎都无法将这些值传递给他的post请求。我尝试了从截击请求到JSON解析的所有方法,但我无法让它工作
这是他的PHP文件
<?php
ini_set('display_errors', 1);
header('Content-type: text/html; charset=utf-8');
//$user = (int)$_POST['user_id'];
$email = $_POST['email'];
$password1 = $_POST['psw'];
$password2 = $_POST['psw-repeat'];
$first = $_POST['first_name'];
$last = $_POST['last_name'];
//<input type="checkbox" checked="checked"> Remember me
include 'connRW.php';
$stmt = $connRW->prepare("SELECT * FROM AdUsers WHERE email = :email or
fb_email
= :fb_email");
$stmt->bindValue(':email', $email);
$stmt->bindValue(':fb_email', $email);
$stmt->execute();
$row1 = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt = $connRW->prepare("SELECT * FROM AdPublishers WHERE email = :email or
fb_email = :fb_email");
$stmt->bindValue(':email', $email);
$stmt->bindValue(':fb_email', $email);
$stmt->execute();
$row2 = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row1 && !$row2 && $password1)
{
if ($password1 == $password2) {
$pwsaltedhashed = password_hash($password1, PASSWORD_DEFAULT);
$stmt = $connRW->prepare("SELECT raadz_user_id FROM GetNext");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$next_user_id = $row["raadz_user_id"] + 1;
$stmt = $connRW->prepare("UPDATE GetNext SET raadz_user_id =
:next_user_id");
$stmt->bindValue(':next_user_id', $next_user_id, PDO::PARAM_INT);
$stmt->execute();
$stmt = $connRW->prepare("INSERT INTO AdUsers ( raadz_user_id, email,
password, first_name, last_name )
VALUES ( :user_id, :email, :password, :first_name, :last_name
)");
$stmt->bindValue(':user_id', $next_user_id, PDO::PARAM_INT);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':password', $pwsaltedhashed);
$stmt->bindValue(':first_name', $first);
$stmt->bindValue(':last_name', $last);
$stmt->execute();
$confirm_string = $next_user_id . substr($first, 0, 2) . substr($last,
0, 2);
这是我在Android Studio中使用的Java类:
public class RegisterActivity extends AppCompatActivity {
TextView tvLogin;
Button bRegister;
EditText etEmail;
EditText etPassword;
EditText etPasswordRepeat;
EditText etfirstName;
EditText etlastName;
String url = "https://example.com/usersignup.php";
String gEmail;
String gPassword;
String gPasswordRepeat;
String gFirst;
String gLast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
etEmail = (EditText) findViewById(R.id.etEmail);
etPassword = (EditText) findViewById(R.id.etPassword);
etPasswordRepeat = (EditText) findViewById(R.id.etPasswordRepeat);
etfirstName = (EditText) findViewById(R.id.firstName);
etlastName = (EditText) findViewById(R.id.lastName);
tvLogin = (TextView) findViewById(R.id.tvLogin);
bRegister = (Button) findViewById(R.id.bRegister);
gEmail = etEmail.getText().toString();
gPassword = etPassword.getText().toString();
gPasswordRepeat = etPasswordRepeat.getText().toString();
gFirst = etfirstName.getText().toString();
gLast = etlastName.getText().toString();
tvLogin.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivity(intent);
}
});
bRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://raadz.com/usersignup.php";
StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.contains("Message could not be send.")) {
Toast.makeText(getApplicationContext(), "Message not sent", Toast.LENGTH_SHORT).show();
}
if(response.contains("Message has been sent")){
Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_SHORT).show();
}
//This code is executed if the server responds, whether or not the response contains data.
//The String 'response' contains the server's response.
}
}, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
//This code is executed if there is an error.
}
}) {
protected Map<String, String> getParams() {
Map<String, String> MyData = new HashMap<String, String>();
MyData.put("email", gEmail);
MyData.put("psw", gPassword);
MyData.put("psw-repeat", gPasswordRepeat);
MyData.put("first_name", gFirst);
MyData.put("last_name", gLast);
return MyData;
}
};
MySingleton.getInstance(getAppContext()).addToRequestQueue(MyStringRequest);
}
});
}
}
我做错了什么
# 1 楼答案
在代码中没有太多的内容:这样的问题本身是错误的
你的Android应用程序无法与“PHP文件”通信
它可以做的是调用/使用用PHP实现的restful web服务。然而,由于您的通信协议是HTTP,所以restful API背后隐藏的技术(可以是PHP、Java、Erlang、Python等等)无关紧要,更不用说理解代码了
尽管如此,你可能想搜索“android call restful service”或诸如此类的服务,可以找到很多例子