使用android

时间:2015-07-09 21:53:14

标签: php android android-studio database-connection

所以我有一个班级注册,我想将用户信息注册到外部数据库,但由于某种原因它不会插入,如果有任何错误或者告诉我什么丢失,请指出错误吗?

这是我将其上传到网络主机

的php文件
<?php

$con = mysqli_connect("localhost","MY_username","My_Password","My_Databas");//my_user, my_password, my_database

$BN = $_POST["BadgeNumber"];
$name = $_POST["Name"];
$EN = $_POST["ExtensionNumber"];
$PN = $_POST["PhoneNumber"];
$email = $_POST["Email"];

$statement = mysqli_prepare($con, "INSERT INTO Register (BadgeNumber, Name, ExtensionNumber, PhoneNumber, Email)
    VALUES (?, ?, ?, ?, ?)");

mysqli_stmt_bind_param($statement, "isiis", $BN, $name, $EN, $PN, $email);  
mysqli_stmt_execute($statement);

mysqli_stmt_close($statement);

mysqli_close($con); ?>

这是发生所有连接和插入的类,我将域名写为SERVER ADDRESS

public class ServerRequests {

private static final String TAG ="ServerRequests";

ProgressDialog progressDialog;
public static final int CONNECTION_TIMEOUT = 1000 * 15;
public static final String SERVER_ADDRESS = "http://92211app.netne.net/";

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

public void StoreUserDataInBackground(User user, GetUserCallback userCallback){

    progressDialog.show();
    new StoreUserDataAsyncTask(user, userCallback).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) {

        //setting the NameValuePairs
        ArrayList<NameValuePair> dataToSend = new ArrayList<>();//stores key and value

        dataToSend.add(new BasicNameValuePair("BadgeNumber",user.getBadgeNumber()+""));
        dataToSend.add(new BasicNameValuePair("Name",user.getName()));
        dataToSend.add(new BasicNameValuePair("ExtensionNumber",user.getExtensionNumber()+""));
        dataToSend.add(new BasicNameValuePair("PhoneNumber",user.getPhoneNumber()+""));
        dataToSend.add(new BasicNameValuePair("Email",user.getEmail()));

        Log.i(TAG,"StoreUserDataAsyncTask method BadgeNumber: "+user.getBadgeNumber()+" Name: "+user.getName());

        HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);//the time we want to wait until the pulse is executed (stop connection)
        HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);//the time we want to wait to receive anything from the server

        HttpClient client = new DefaultHttpClient(httpRequestParams);//allows us to make requests to the server, we passed httpRequestParams so the client would know the connection timeout
        HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");

        try{
            post.setEntity(new UrlEncodedFormEntity(dataToSend));
            client.execute(post);
            Log.i(TAG,"try and catch");
        }catch(Exception e){
            e.printStackTrace();
        }


        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid){
        progressDialog.dismiss();
        userCallback.done(null);
        Log.i(TAG,"onPostExecute");
        super.onPostExecute(aVoid);
    }
}

这是界面

interface GetUserCallback {

public abstract void done(User returnedUser);

}

这是我点击按钮后调用StoreUserDataInBackground方法的地方

 RegisterButton.setOnClickListener(new View.OnClickListener() {

        //InputStream is = null;

        @Override
        public void onClick(View v) {
            int BN = Integer.parseInt(BadgeNumberView.getText().toString());
            String name = NameView.getText().toString();
            int EN = Integer.parseInt(ExtensionNumberView.getText().toString());
            int PN = Integer.parseInt(PhoneNumberView.getText().toString());
            String Email = EmailView.getText().toString();

                User user = new User(BN, name, EN, PN, Email);

                registerUser(user);

        }
    });   

private void registerUser(User user){

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

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

        }
    });
}

1 个答案:

答案 0 :(得分:0)

1.“电子邮件”似乎是保留的,将其更改为其他内容。

2.试试这个:

     dataToSend.add(new BasicNameValuePair("BadgeNumber",user.BN +""));
    dataToSend.add(new BasicNameValuePair("Name",user.name));
    dataToSend.add(new BasicNameValuePair("ExtensionNumber",user.EN +""));
    dataToSend.add(new BasicNameValuePair("PhoneNumber",user.PN +""));
    dataToSend.add(new BasicNameValuePair("Email",user.e_mail));

    Log.i(TAG,"StoreUserDataAsyncTask method BadgeNumber: "+user.BN +" Name: "+user.name);