让Android App与PHP文件通信

时间:2017-10-04 12:40:46

标签: java php android database

我正在尝试让我的Android应用程序与我的朋友PHP文件进行通信,该文件将数据存储到我们的数据库中,然后将该数据注册到新的用户帐户。我是PHP的新手,但我可以阅读它。但无论我尝试什么,似乎没有任何东西可以将值传递给他的帖子请求。我已经尝试过从Volley Requests到JSON Parsing的所有内容,我无法让它工作。

这是他的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 个答案:

答案 0 :(得分:1)

没有太多的代码:这样的问题是错误的。

您的Android应用无法与&#34; PHP文件&#34;。

进行通信

它可以做的是调用/使用PHP实现的restful Web服务。然而,由于您的通信协议是HTTP,因此隐藏在其余API(可能是PHP,Java,Erlang,Python ......等等)背后的技术并不重要,更不用说理解代码了。

话虽这么说,你可能想要搜索&#34; android调用restful service&#34;或者类似的东西 - 可以找到很多例子。