OkHttp无法发布到mysql

问题描述 投票:0回答:1

我在这里有问题..我的应用程序应该从文本框etName,etEmail和etPassword发送3个值到数据库

但相反,它不发送任何信息...我是android的新手,在我跟随一些教程时,我不知道自己在代码中写的一些内容

这在我的注册代码中

package com.xulucreatives.taxisiyaya;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {

EditText etName,etEmail,etPassword;
Button btnReg;

final String url_Register ="http://taxinote.000webhostapp.com/register_user.php";
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        etName = findViewById(R.id.RM);
        etEmail = findViewById(R.id.et_email);
        etPassword = findViewById(R.id.et_password);
        btnReg = findViewById(R.id.btn_reg);

        btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String Name = etName.getText().toString();
                String Email = etEmail.getText().toString();
                String Password = etPassword.getText().toString();

                new RegisterUser().execute(Name,Email,Password);
               // Toast.makeText(RegisterActivity.this,"Im working you bitch",Toast.LENGTH_LONG).show();
            }
        });
    }
    public class RegisterUser extends AsyncTask<String,Void,String>{
        @Override
        protected String doInBackground(String... strings) {
            String Name = strings[0];
            String Email = strings[1];
            String Password = strings[2];
            String finalURL = url_Register + "?user_name" + Name +
                    "&user_id"+ Email +
                    "&user_password"+ Password;

            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(finalURL)

                    .build();

            Response response = null;

            try{
                response = okHttpClient.newCall(request).execute();

                if(response.isSuccessful())

                {
                    String result = response.body().string();
                    if(result.equalsIgnoreCase("uaser registered successfully"))
                    {
                        Toast.makeText(RegisterActivity.this,"Registered Successfully",Toast.LENGTH_LONG).show();

                        Intent i = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(i);
                        finish();
                    }
                    else if(result.equalsIgnoreCase("user already exists")){

                        Toast.makeText(RegisterActivity.this,"User Already Exists",Toast.LENGTH_LONG).show();


                    }
                    else
                    {
                        Toast.makeText(RegisterActivity.this,"Ooops ! Ran into a problem , try again",Toast.LENGTH_LONG).show();
                    }
                }
                else{
                    Toast.makeText(RegisterActivity.this,"response not successful!! :/",Toast.LENGTH_LONG).show();
                }

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

            }

            return null;
        }
    }
    public void showToast(final String Text){
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(RegisterActivity.this,Text,Toast.LENGTH_LONG).show();
            }
        });
    }
}

user_name,user_id和user_password来自服务器中我的php文件

<?php
require_once('connect.php');

$userName = $_GET['user_name'];
$userID = $_GET['user_id'];
$userPassword = $_GET['user_password'];


    $query = "select * from users where email = '$userID'";
    $recordExists = mysqli_fetch_array(mysqli_query($conn, $query));

    if(isset($recordExists)){
        echo 'User already exists';

    }else{
        $query = "INSERT INTO users (name, email, password) VALUES ('$userName', '$userID', '$userPassword')";

        if(mysqli_query($conn, $query)){

            echo 'User registered successfully';

        }else{
            echo 'oops! please try again!';
        }
    }

?>


但是它不起作用,我不知道为什么

java php android okhttp
1个回答
0
投票

您创建的网址无效这是

 String finalURL = url_Register + "?user_name" + Name +
                "&user_id"+ Email +
                "&user_password"+ Password;

并且应该像这样

 String finalURL = url_Register + "?user_name=" + Name +
                "&user_id="+ Email +
                "&user_password="+ Password;
© www.soinside.com 2019 - 2024. All rights reserved.