php - Android注册表单使用php和mysql

标签 php android mysql registration

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET" />
    
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        
        >
        <activity
            android:name="com.example.helloworld.Login"
            android:label="@string/app_name"
            
            > 
            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.helloworld.Userpage"
            android:label="@string/app_name">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
          <activity
            android:name="com.example.helloworld.Register"
            android:label="@string/app_name">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我正在使用 php 和 mysql 开发 android 注册页面。有一些运行时异常。按提交后我得到异常空。我怎样才能摆脱这个问题。谁能帮我?提前致谢...

10-27 02:03:50.494: W/SingleClientConnManager(1904): Make sure to release the connection before allocating another one.
10-27 02:03:50.784: I/System.out(1904): Response : User Found
10-27 02:03:51.234: D/dalvikvm(1904): GC_FOR_ALLOC freed 354K, 8% free 4980K/5404K, paused 120ms, total 167ms
10-27 02:03:51.554: I/Choreographer(1904): Skipped 64 frames!  The application may be doing too much work on its main thread.
10-27 02:03:57.154: I/Choreographer(1904): Skipped 537 frames!  The application may be doing too much work on its main thread.
10-27 02:06:58.474: I/Choreographer(1904): Skipped 393 frames!  The application may be doing too much work on its main thread.
10-27 02:07:06.234: I/Choreographer(1904): Skipped 69 frames!  The application may be doing too much work on its main thread.
10-27 03:35:10.264: W/IInputConnectionWrapper(1904): showStatusIcon on inactive InputConnection
10-27 03:57:59.544: D/dalvikvm(2149): GC_FOR_ALLOC freed 91K, 5% free 3285K/3444K, paused 33ms, total 37ms
10-27 03:57:59.584: D/gralloc_goldfish(2149): Emulator without GPU emulation detected.
10-27 03:58:02.324: D/AndroidRuntime(2149): Shutting down VM
10-27 03:58:02.324: W/dalvikvm(2149): threadid=1: thread exiting with uncaught exception (group=0xb1a20ba8)
10-27 03:58:02.364: E/AndroidRuntime(2149): FATAL EXCEPTION: main
10-27 03:58:02.364: E/AndroidRuntime(2149): Process: com.example.helloworld, PID: 2149
10-27 03:58:02.364: E/AndroidRuntime(2149): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.helloworld/com.example.helloworld.Register}; have you declared this activity in your AndroidManifest.xml?
10-27 03:58:02.364: E/AndroidRuntime(2149):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at android.app.Activity.startActivityForResult(Activity.java:3424)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at android.app.Activity.startActivityForResult(Activity.java:3385)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at android.app.Activity.startActivity(Activity.java:3627)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at android.app.Activity.startActivity(Activity.java:3595)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at com.example.helloworld.Login$2.onClick(Login.java:66)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at android.view.View.performClick(View.java:4438)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at android.view.View$PerformClick.run(View.java:18422)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at android.os.Handler.handleCallback(Handler.java:733)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at android.os.Handler.dispatchMessage(Handler.java:95)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at android.os.Looper.loop(Looper.java:136)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at java.lang.reflect.Method.invokeNative(Native Method)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at java.lang.reflect.Method.invoke(Method.java:515)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-27 03:58:02.364: E/AndroidRuntime(2149):     at dalvik.system.NativeStart.main(Native Method)
10-27 03:58:12.294: I/Process(2149): Sending signal. PID: 2149 SIG: 9
10-27 03:59:40.824: D/dalvikvm(2202): GC_FOR_ALLOC freed 94K, 5% free 3286K/3448K, paused 26ms, total 29ms
10-27 03:59:40.904: D/gralloc_goldfish(2202): Emulator without GPU emulation detected.
10-27 03:59:46.634: I/Choreographer(2202): Skipped 184 frames!  The application may be doing too much work on its main thread.
10-27 03:59:54.114: D/InputEventConsistencyVerifier(2202): KeyEvent: ACTION_UP but key was not down.
10-27 03:59:54.114: D/InputEventConsistencyVerifier(2202):   in android.widget.EditText{b1d1d568 VFED..CL .F....I. 0,191-704,270 #7f09000a app:id/age}
10-27 03:59:54.114: D/InputEventConsistencyVerifier(2202):   0: sent at 13917600000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=13917600, downTime=13917519, deviceId=0, source=0x101 }
10-27 03:59:56.604: D/InputEventConsistencyVerifier(2202): KeyEvent: ACTION_UP but key was not down.
10-27 03:59:56.604: D/InputEventConsistencyVerifier(2202):   in android.widget.EditText{b1d1eee8 VFED..CL .F....I. 0,310-704,389 #7f090002 app:id/username}
10-27 03:59:56.604: D/InputEventConsistencyVerifier(2202):   0: sent at 13920082000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=13920082, downTime=13920007, deviceId=0, source=0x101 }
10-27 03:59:59.344: D/InputEventConsistencyVerifier(2202): KeyEvent: ACTION_UP but key was not down.
10-27 03:59:59.344: D/InputEventConsistencyVerifier(2202):   in android.widget.EditText{b1d205d8 VFED..CL .F....I. 0,429-704,508 #7f090004 app:id/password}
10-27 03:59:59.344: D/InputEventConsistencyVerifier(2202):   0: sent at 13922898000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=13922898, downTime=13922804, deviceId=0, source=0x101 }
10-27 04:00:02.564: D/dalvikvm(2202): GC_FOR_ALLOC freed 146K, 6% free 3621K/3836K, paused 25ms, total 28ms
10-27 04:00:02.564: I/dalvikvm-heap(2202): Grow heap (frag case) to 4.675MB for 1127536-byte allocation
10-27 04:00:02.604: D/dalvikvm(2202): GC_FOR_ALLOC freed 2K, 5% free 4720K/4940K, paused 31ms, total 31ms
10-27 04:00:02.754: I/System.out(2202): Exception : null
10-27 04:00:02.754: W/System.err(2202): java.lang.NullPointerException
10-27 04:00:02.764: W/System.err(2202):     at com.example.helloworld.Register.signup(Register.java:65)
10-27 04:00:02.764: W/System.err(2202):     at com.example.helloworld.Register$1$1.run(Register.java:49)
10-27 04:00:02.764: W/System.err(2202):     at java.lang.Thread.run(Thread.java:841)
<?php
$hostname_localhost ="localhost";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect("localhost","root","","mydatabase")
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db("mydatabase", $localhost);

$username = $_POST['username'];
$password = $_POST['password'];
$name = $_POST['name'];
$age = $_POST['age'];
$sql = "Insert into `user` values ('$username','$password','$name','$age')";
if (mysql_query($localhost, $sql)) {
    echo "New record created successfully";
} 
else {
    echo "Error: " . $sql . "<br>" . mysql_error($conn);
}
?>
package com.example.helloworld;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Register extends Activity{
    Button bt;
    TextView tv;
    EditText uname,pass,name,age;
    HttpPost httppost1;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient1;
    List<NameValuePair> nameValuePairs1;
    ProgressDialog dialog=null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        bt= (Button)findViewById(R.id.submit);

        bt.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 dialog = ProgressDialog.show(Register.this, "", 
                         "Registering...", true);
                  new Thread(new Runnable() {
                         public void run() {
                             signup();                          
                         }
                       }).start();               
             }
    });

    }
    void signup(){
        try{            

            httpclient1=new DefaultHttpClient();
            httppost1= new HttpPost("http://10.0.2.2/my_folder_inside_htdocs/insert.php"); // make sure the url is correct.
            //add your data
            nameValuePairs1 = new ArrayList<NameValuePair>();
            // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 

            nameValuePairs1.add(new BasicNameValuePair("username",uname.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
            nameValuePairs1.add(new BasicNameValuePair("password",pass.getText().toString().trim())); 
            nameValuePairs1.add(new BasicNameValuePair("name",name.getText().toString().trim())); 
            nameValuePairs1.add(new BasicNameValuePair("age",age.getText().toString().trim()));


            httppost1.setEntity(new UrlEncodedFormEntity(nameValuePairs1));
            //Execute HTTP Post Request
            response=httpclient1.execute(httppost1);
            // e.. from here....
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient1.execute(httppost1, responseHandler);
            System.out.println("Response : " + response); 

            runOnUiThread(new Runnable() {
                public void run() {
                    tv.setText("Response from PHP : " + response);
                    dialog.dismiss();
                }
            });

            if(response.equalsIgnoreCase("User Registered")){
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(Register.this,"Registration Success", Toast.LENGTH_SHORT).show();
                    }
                });

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


        }      
        catch(Exception e){
            dialog.dismiss();
            System.out.println("Exception : " + e.getMessage());
            e.printStackTrace();
        }
    }
     public void showAlert(){
            Register.this.runOnUiThread(new Runnable() {
                public void run() {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Register.this);
                    builder.setTitle("Registration Error.");
                    builder.setMessage("Can not register.")  
                           .setCancelable(false)
                           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id) {
                               }
                           });                     
                    AlertDialog alert = builder.create();
                    alert.show();               
                }
            });
        }

}

最佳答案

您还没有获得对以下 EditText uname,pass,name,age 的引用。在使用它们之前,您必须使用 findViewById 获取它们的引用:-/

关于php - Android注册表单使用php和mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26584855/

相关文章:

php - 变量 $_GET 变量

android - 有没有办法在 RecyclerView 中使用多个 LayoutManagers 和 MergeAdapter?

android - 控制音乐播放器服务 fragment 中的播放暂停按钮

php - 以编程方式添加带有附件的 Wordpress 帖子

php - Laravel sql搜索json类型列

android - setPreviewCallback() 和 setPreviewCallbackWithBuffer()

java - 他们为什么使用本指南中的界面

使用日语文件名上传的 PHP 文件在数据库中变成损坏的文件名

php - 使用mysql数据库中的类别名称填充选择下拉菜单

php - 用 PHP 解析 XML