android studio无法连接到在线数据库

时间:2017-08-29 08:04:39

标签: javascript php android database

1.所以我按照Tonikami教程,尝试在000webhost和awardspace上的数据库中添加行,但没有成功。我只是按下注册按钮,没有任何反应。在显示器上,我收到一条消息No Network Security Config,使用平台默认设置,互联网上有很多人说我不应该因此而遇到问题。我将上传我的注册请求活动,我将其命名为register-page,其中包含register-activity和register.php。请记住,我已经有一个登录页面和一个login.php但是虽然注册表没有工作,但我还是不想询问它们。我还检查了php代码是否有效www.mywebsite.com/Register.php,它可以很好地在我的数据库中添加一行。 Android版本7.1.1。

RegisterPage(请求)

package com.example.geqwe1.atry;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterPage extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_page);


        ArrayAdapter<CharSequence> adapter;
        Button register_b;
        Button previous;

        final Spinner spinner = (Spinner) findViewById(R.id.gender);
        register_b = (Button) findViewById(R.id.register_button);
        previous = (Button) findViewById(R.id.previous_button);
        final EditText email_1 = (EditText) findViewById(R.id.email_register);


        adapter = ArrayAdapter.createFromResource(this, R.array.Gender, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        register_b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String email = email_1.getText().toString();
                final String spinner1 = spinner.getSelectedItem().toString();

                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");
                            //Toast.makeText(RegisterPage.this,"success",Toast.LENGTH_SHORT).show();

                            if(success) {
                                Intent successIntent = new Intent(RegisterPage.this,LoginActivity.class);
                                startActivity(successIntent);
                            }
                            else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterPage.this);
                                builder.setMessage("Register failed")
                                            .setNegativeButton("Retry",null)
                                            .create()
                                            .show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                };

                RegisterActivity registerRequest = new RegisterActivity(email,spinner1,responseListener);
                RequestQueue queue = Volley.newRequestQueue(RegisterPage.this);
                queue.add(registerRequest);
            }
        });

        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent previousIntent = new Intent(RegisterPage.this,LoginActivity.class);
                startActivity(previousIntent);
            }
        });

    }
};

RegisterActivity

package com.example.geqwe1.atry;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

public class RegisterActivity extends StringRequest{
    private static final String REGISTER_REQUEST_URL = "http://www.ankle.atwebpages.com/Register.php";
    private Map<String, String> params;

    public RegisterActivity(String email, String gender, Response.Listener<String> listener){
        super(Method.POST,REGISTER_REQUEST_URL,listener,null);
        params = new HashMap<>();
        params.put("email",email);
        params.put("gender",gender + "");
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

2.我已将此行放入Android Manifest

<uses-permission android:name="android.permission.INTERNET" />

Register.php

<?php
$con = mysqli_connect("fdb15.awardspace.net", "2432552_db", "mypassword", "2432552_db");

$email = $_POST["email"];
$gender = $_POST["gender"];
$statement = mysqli_prepare($con, "INSERT INTO users_table (email, gender) VALUES (?, ?)");
mysqli_stmt_bind_param($statement, "ss", $email, $gender);
mysqli_stmt_execute($statement);

$response = array();
$response["success"] = true;  

echo json_encode($response);
?>

提前感谢您的建议。

0 个答案:

没有答案