Android Studio登录创建帐户

I was following a tutorial in YouTube but I only type the login and the create account part.

So my question is there is no error on my program but i just can't insert new data into my database. I am using wamp as my server. Below is my class to send requests to the database

    package com.example.brian.errandangels;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Brian on 2015/12/31.
 */
public class ServerRequests {
    ProgressDialog progressDialog;
    public static final int CONNECTION_TIMEOUT=1000 * 15;
    public static final String SERVER_ADDRESS = "https://192.168.0.2/";


   public ServerRequests(Context context){
       progressDialog = new ProgressDialog(context);
       progressDialog.setCancelable(false);
       progressDialog.setTitle("Processing");
       progressDialog.setMessage("Please wait....");
   }

    public void storeUserDataInBackground(User user, GetUserCallback callback){
        progressDialog.show();
        new StoreUserDataAsyncTask(user, callback).execute();
    }

    public void fetchUserDataInbackground(User user, GetUserCallback callback) {
        progressDialog.show();
        new fetchUserDataAsyncTask(user, callback).execute();
}

    public class StoreUserDataAsyncTask extends AsyncTask<Void, Void, Void> {
        User user;

        GetUserCallback userCallback;

        public StoreUserDataAsyncTask(User user, GetUserCallback userCallback) {
            this.user = user;
            this.userCallback = userCallback;

        }

        @Override
        protected Void doInBackground(Void... params) {
            ArrayList<NameValuePair> dateToSend = new ArrayList<>();
            dateToSend.add(new BasicNameValuePair("name", user.name));
            dateToSend.add(new BasicNameValuePair("account", user.account));
            dateToSend.add(new BasicNameValuePair("password", user.password));
            dateToSend.add(new BasicNameValuePair("phone", user.phone));
            dateToSend.add(new BasicNameValuePair("department", user.department));

            HttpParams httpRequestParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
            HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);

            HttpClient Client = new DefaultHttpClient(httpRequestParams);
            HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");

            try {
                post.setEntity(new UrlEncodedFormEntity(dateToSend));
                Client.execute(post);

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

        }

        @Override
        protected void onPostExecute(Void aVoid) {
            progressDialog.dismiss();
            userCallback.done(null);
            super.onPostExecute(aVoid);
        }
    }
            public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User>{
            User user;
            GetUserCallback userCallback;
            public fetchUserDataAsyncTask(User user, GetUserCallback userCallback){
                this.user = user;
                this.userCallback = userCallback;
            }

            protected User doInBackground(Void... params) {

                ArrayList<NameValuePair> dateToSend = new ArrayList<>();
                dateToSend.add(new BasicNameValuePair("account", user.account));
                dateToSend.add(new BasicNameValuePair("password", user.password));

                HttpParams httpRequestParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
                HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);

                HttpClient Client = new DefaultHttpClient(httpRequestParams);
                HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");

                User returnedUser = null;
                try {
                    post.setEntity(new UrlEncodedFormEntity(dateToSend));
                    HttpResponse httpResponse = Client.execute(post);

                    HttpEntity entity = httpResponse.getEntity();
                    String result = EntityUtils.toString(entity);
                    JSONObject jObject = new JSONObject(result);
                    if (jObject.length() == 0) {
                        user = null;

                    }else {
                        String name = jObject.getString("name");
                        String phone = jObject.getString("phone");
                        String department = jObject.getString("department");
                        returnedUser = new User(name, phone, department, user.account, user.password);
                    }

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



                return returnedUser;
            }
            @Override
            protected void onPostExecute(User returnedUser) {
                progressDialog.dismiss();
                userCallback.done(returnedUser);
                super.onPostExecute(returnedUser);
            }



        }
    }

Is my address wrong? I'm not sure.

Bellow is my login page

   package com.example.brian.errandangels;

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

    public class Login extends AppCompatActivity implements 

View.OnClickListener {

    Button btnLogin;
    EditText account, password;
    TextView Register;

    UserLocalStore userLocalStore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        account = (EditText)findViewById(R.id.account);
        password = (EditText)findViewById(R.id.password);
        btnLogin = (Button)findViewById(R.id.btnLogin);
        Register = (TextView)findViewById(R.id.Register);

        btnLogin.setOnClickListener(this);
        Register.setOnClickListener(this);

        userLocalStore = new UserLocalStore(this);
    }


    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btnLogin:
                String etaccount = account.getText().toString();
                String etpassword = password.getText().toString();

                User user = new User( etaccount, etpassword);
                authenticate(user);

            break;

            case R.id.Register:

            startActivity(new Intent(this, Register.class));
            break;
        }

    }

    private void authenticate(User user){
        ServerRequests serverRequests = new ServerRequests(this);
        serverRequests.fetchUserDataInbackground(user, new GetUserCallback() {
            @Override
            public void done(User returnedUser) {

                if (returnedUser == null) {
                    showErrorMessage();
                }else {
                    logUserIn(returnedUser);
                }
            }
        });
    }
    private void showErrorMessage(){
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Login.this);

        dialogBuilder.setMessage("Incorrect user details");
        dialogBuilder.setPositiveButton("ok", null);
        dialogBuilder.show();
    }
    private void logUserIn(User returnedUser){
        UserLocalStore.StoreUserData(returnedUser);
        UserLocalStore.setUserLoggedIn(true);

        startActivity(new Intent(this, MainActivity.class));
    }


}


This is the create account page 





package com.example.brian.errandangels;

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

public class Register extends AppCompatActivity implements View.OnClickListener {

    Button btnRC;
    EditText etname, etaccount, etpassword, etphone, etdepartment;



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

        etname = (EditText)findViewById(R.id.name);
       etaccount = (EditText)findViewById(R.id.account);
        etpassword = (EditText)findViewById(R.id.password);
        etphone = (EditText)findViewById(R.id.phone);
        etdepartment = (EditText)findViewById(R.id.department);
        btnRC = (Button)findViewById(R.id.btnRC);

        btnRC.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btnRC:
                String name = etname.getText().toString();
                String account = etaccount.getText().toString();
                String password = etpassword.getText().toString();
                String phone = etphone.getText().toString();
                String department = etdepartment.getText().toString();

                User user = new User(name,account,password,phone,department);

                registerUser(user);
                break;


        }
    }

    private void registerUser(User user){
        ServerRequests serverRequests = new ServerRequests(this);
        serverRequests.storeUserDataInBackground(user, new GetUserCallback() {

            @Override
            public void done(User returnedUser) {

                    startActivity(new Intent(Register.this, Login.class));
            }
        });
    }

}

and this is my php file

<?php

    $con=mysqli_connect("localhost", "root","","seniorproject");

    $name = $_POST["name"];
    $account = $_POST["account"];
    $password = $_POST["password"];
    $phone = $_POST["phone"];
    $department = $_POST["department"];

    $statement = mysqli_prepare($con, "INSERT INTO User (name, account, password, phone, department) VALUES(?, ?, ?, ?, ?) ");
    mysqli_stmt_bind_param($statement, "sssss", $name , $account, $password, $phone, $department );
    mysqli_stmt_execute($statement);

    mysqli_stmt_close($statement);


    mysqli_close($con);
?>

Please help me understand what's wrong with my program

(For simplicity I just make all my column in the database into varchar and everything into string in the program)

01-01 06:03:38.774 15121-15578/org.pickyou.loginandregisterv1 W/System.err: org.apache.http.conn.ConnectTimeoutException: Connect to /192.168.0.2:443 timed out
01-01 06:03:38.774 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
01-01 06:03:38.774 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
01-01 06:03:38.774 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-01 06:03:38.774 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-01 06:03:38.778 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-01 06:03:38.778 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-01 06:03:38.778 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-01 06:03:38.778 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-01 06:03:38.778 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at com.example.brian.errandangels.ServerRequests$StoreUserDataAsyncTask.doInBackground(ServerRequests.java:78)
01-01 06:03:38.778 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at com.example.brian.errandangels.ServerRequests$StoreUserDataAsyncTask.doInBackground(ServerRequests.java:49)
01-01 06:03:38.778 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-01 06:03:38.778 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-01 06:03:38.778 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-01 06:03:38.778 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-01 06:03:38.778 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-01 06:03:38.778 15121-15578/org.pickyou.loginandregisterv1 W/System.err:     at java.lang.Thread.run(Thread.java:856)

this is the logcat after i press create account in emulator

dsoxcj7276
dsoxcj7276 您必须逐步检查,在哪里遇到问题。检查你的PHP代码。它有效吗?
4 年多之前 回复
dsk49208
dsk49208 不,我没有得到任何错误,但当我点击创建帐户时,数据库永远不会插入任何数据所以我想知道出了什么问题
4 年多之前 回复
duanbi3151
duanbi3151 你怎么了。你收到错误了吗?
4 年多之前 回复

1个回答



验证192.168.0.2实际上是IP地址,因为错误消息清楚地报告了尝试连接到该终点的超时。</ p >
</ div>

展开原文

原文

Verify 192.168.0.2 is actually the ip address as the error message clearly reports a timeout trying to connect to that end point.

dsgnsdjk26586
dsgnsdjk26586 你能使用chrome从设备打开php脚本吗? 如果失败,则表明设备正在使用的LAN,子网,网关地址上存在DNS问题。
4 年多之前 回复
dongzhan1383
dongzhan1383 谢谢,我多次检查了我的IP地址
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐