0

我是 Android 编程新手。

我现在正在尝试将新记录从我的 Android 应用程序注册到我的本地主机服务器...我按照http://www.androidhive.info/2012/05/how-to-connect-android-with-php上的教程进行操作-mysql/

我的代码都很好,我可以像往常一样运行我的应用程序,但是在我单击“注册”按钮后整个应用程序立即停止了

下面是我的日志:

05-13 19:36:24.990: E/Trace(31448): error opening trace file: No such file or directory (2)
05-13 19:36:25.425: D/libEGL(31448): loaded /system/lib/egl/libEGL_mali.so
05-13 19:36:25.455: D/libEGL(31448): loaded /system/lib/egl/libGLESv1_CM_mali.so
05-13 19:36:25.465: D/libEGL(31448): loaded /system/lib/egl/libGLESv2_mali.so
05-13 19:36:25.485: E/(31448): Device driver API match
05-13 19:36:25.485: E/(31448): Device driver API version: 17
05-13 19:36:25.485: E/(31448): User space API version: 17 
05-13 19:36:25.485: E/(31448): mali: REVISION=Linux-r3p1-01rel1 BUILD_DATE=Thu Jan 24 17:16:14 KST 2013 
05-13 19:36:25.600: D/OpenGLRenderer(31448): Enabling debug mode 0
05-13 19:36:43.995: W/IInputConnectionWrapper(31448): getCursorCapsMode on inactive InputConnection
05-13 19:36:46.570: W/dalvikvm(31448): threadid=12: thread exiting with uncaught exception (group=0x40dff930)
05-13 19:36:46.585: E/AndroidRuntime(31448): FATAL EXCEPTION: AsyncTask #1
05-13 19:36:46.585: E/AndroidRuntime(31448): java.lang.RuntimeException: An error occured while executing doInBackground()
05-13 19:36:46.585: E/AndroidRuntime(31448):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at java.lang.Thread.run(Thread.java:856)
05-13 19:36:46.585: E/AndroidRuntime(31448): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=http//10.0.2.2/iChop/signup.php
05-13 19:36:46.585: E/AndroidRuntime(31448):    at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at com.example.ichop.JSONParser.makeHttpRequest(JSONParser.java:51)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at com.example.ichop.SignUp$CreateNewCustomer.doInBackground(SignUp.java:90)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at com.example.ichop.SignUp$CreateNewCustomer.doInBackground(SignUp.java:1)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-13 19:36:46.585: E/AndroidRuntime(31448):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-13 19:36:46.585: E/AndroidRuntime(31448):    ... 4 more
05-13 19:36:47.300: E/WindowManager(31448): Activity com.example.ichop.SignUp has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{410e2958 V.E..... R......D 0,0-684,192} that was originally added here
05-13 19:36:47.300: E/WindowManager(31448): android.view.WindowLeaked: Activity com.example.ichop.SignUp has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{410e2958 V.E..... R......D 0,0-684,192} that was originally added here
05-13 19:36:47.300: E/WindowManager(31448):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
05-13 19:36:47.300: E/WindowManager(31448):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
05-13 19:36:47.300: E/WindowManager(31448):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-13 19:36:47.300: E/WindowManager(31448):     at android.app.Dialog.show(Dialog.java:281)
05-13 19:36:47.300: E/WindowManager(31448):     at com.example.ichop.SignUp$CreateNewCustomer.onPreExecute(SignUp.java:73)
05-13 19:36:47.300: E/WindowManager(31448):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
05-13 19:36:47.300: E/WindowManager(31448):     at android.os.AsyncTask.execute(AsyncTask.java:534)
05-13 19:36:47.300: E/WindowManager(31448):     at com.example.ichop.SignUp$1.onClick(SignUp.java:59)
05-13 19:36:47.300: E/WindowManager(31448):     at android.view.View.performClick(View.java:4204)
05-13 19:36:47.300: E/WindowManager(31448):     at android.view.View$PerformClick.run(View.java:17355)
05-13 19:36:47.300: E/WindowManager(31448):     at android.os.Handler.handleCallback(Handler.java:725)
05-13 19:36:47.300: E/WindowManager(31448):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 19:36:47.300: E/WindowManager(31448):     at android.os.Looper.loop(Looper.java:137)
05-13 19:36:47.300: E/WindowManager(31448):     at android.app.ActivityThread.main(ActivityThread.java:5226)
05-13 19:36:47.300: E/WindowManager(31448):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 19:36:47.300: E/WindowManager(31448):     at java.lang.reflect.Method.invoke(Method.java:511)
05-13 19:36:47.300: E/WindowManager(31448):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-13 19:36:47.300: E/WindowManager(31448):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-13 19:36:47.300: E/WindowManager(31448):     at dalvik.system.NativeStart.main(Native Method)

我应该怎么办?:(

下面是我的注册按钮:

public class SignUp extends Activity {

//Progress dialog
private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
EditText C_Name;
EditText C_Email_SignUp;
EditText C_Password_SignUp;
EditText C_Retype_SignUp;

//url to create new customer
private static String url_create_customer = "http//10.0.2.2/iChop/signup.php";

//JSON Node names
private static final String TAG_SUCCESS = "success";

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

    //Edit Text
    C_Name = (EditText) findViewById(R.id.C_Name);
    C_Email_SignUp = (EditText) findViewById(R.id.C_Email_SignUp);
    C_Password_SignUp = (EditText) findViewById(R.id.C_Password_SignUp);
    C_Retype_SignUp = (EditText) findViewById(R.id.C_Retype_SignUp);

    //Sign Up button
    Button SignUpBtn = (Button) findViewById(R.id.SignUpBtn);

    //Button click event
    SignUpBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // creating new customer in background
            new CreateNewCustomer().execute();              
        }
    });
}

class CreateNewCustomer extends AsyncTask <String, String, String> {
    //show Progress Dialog before starting background thread

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(SignUp.this);
        pDialog.setMessage("Signing up..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected String doInBackground(String... args){
        String name = C_Name.getText().toString();
        String email = C_Email_SignUp.getText().toString();
        String password = C_Password_SignUp.getText().toString();
        String retype = C_Retype_SignUp.getText().toString();

        //Building parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("email",email));
        params.add(new BasicNameValuePair("password",password));
        params.add(new BasicNameValuePair("retype", retype));

        //getting JSON object
        JSONObject json = jsonParser.makeHttpRequest(url_create_customer, "POST", params);

        //check log cat for response
        Log.d("Create Response", json.toString());

        //check for success tag
        try{
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1){
                Intent i = new Intent (getApplicationContext(), Login.class);
                startActivity (i);

                finish();
            }else{
                //failed to create customer
                Intent i = new Intent (getApplicationContext(), SignUp.class);
                startActivity (i);

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

        return null;
    }

    protected void onPostExecute(String file_url){
        pDialog.dismiss();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.sign_up, menu);
    return true;
}

}

接受 bofredo 调整后的最新 LogCat

05-13 20:39:29.210: W/System.err(3258): org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2 refused
05-13 20:39:29.260: W/System.err(3258): Caused by: java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect failed: ETIMEDOUT (Connection timed out)
05-13 20:39:29.290: W/System.err(3258): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
05-13 20:39:29.300: E/Buffer Error(3258): Error converting result java.lang.NullPointerException: lock == null
05-13 20:39:29.305: E/JSON Parser(3258): Error parsing data org.json.JSONException: End of input at character 0 of 
05-13 20:39:29.315: W/dalvikvm(3258): threadid=11: thread exiting with uncaught exception (group=0x40dff930)
05-13 20:39:29.335: E/AndroidRuntime(3258): FATAL EXCEPTION: AsyncTask #1
05-13 20:39:29.335: E/AndroidRuntime(3258): java.lang.RuntimeException: An error occured while executing doInBackground()
05-13 20:39:29.335: E/AndroidRuntime(3258):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-13 20:39:29.335: E/AndroidRuntime(3258): Caused by: java.lang.NullPointerException

接受 tolgap 评论后的最新 logcat

05-13 21:25:09.995: E/JSON Parser(6967): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
05-13 21:25:09.995: W/dalvikvm(6967): threadid=12: thread exiting with uncaught exception (group=0x40dff930)
05-13 21:25:10.015: E/AndroidRuntime(6967): FATAL EXCEPTION: AsyncTask #1
05-13 21:25:10.015: E/AndroidRuntime(6967): java.lang.RuntimeException: An error occured while executing doInBackground()
05-13 21:25:10.015: E/AndroidRuntime(6967):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-13 21:25:10.015: E/AndroidRuntime(6967): Caused by: java.lang.NullPointerException

tolgap 要求的 PHP 代码

    <?php
//Following code will create a new customer row

$response = array();

if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['retype'])){
    $C_Name = $_POST['name'];
    $C_Email_SignUp = $_POST['email'];
    $C_Password_SignUp = $_POST['password'];
    $C_Retype_SignUp = $_POST['retype'];
    $pregmatch = "/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/";

    //include db connect class
    require_once __DIR__ . '/dbconnect.php';

    //connecting to db
    $db = new DB_CONNECT();

    //checking if email has been registered before
    $check = mysql_query("select count(Customer_ID) from customer where C_Email = '".$C_Email_SignUp."'");
    $data = mysql_fetch_array($check);

    if ($data['count (customer_id)'] == 0){
        if ($C_Password_SignUp == $C_Retype_SignUp){
            if (preg_match($pregmatch, $C_Email_SignUp)){
                $result = mysql_query("INSERT INTO customer (Customer_ID, C_name, C_Email, C_Password) VALUES ('', '".$C_Name."', '".$C_Email_SignUp."', '".$C_Password_SignUp."')");

                $response["success"] = 1;
                $response["message"] = "Registration completed!";

                echo json_encode($response);
            } else {
                //error for wrong email format              
                $response["success"] = 0;
                $response["message"] = "Wrong email format!";

                echo json_encode($response);
            }
        } else {
            //error for mismatch password and retype password
            $response["success"] = 0;
            $response["message"] = "Password and retype password is not the same!";

            echo json_encode($response);
        }
    } else {
        //error for registered email
        $response["success"] = 0;
        $response["message"] = "Email has been registered!";

        echo json_encode($response);
    }
    } else {
        //error for blank field
        $response["success"] = 0;
        $response["message"] = "Please fill in all the fields!";
    }
    ?>
4

2 回答 2

1

在您的情况下,将您的 IP 更改为您的 PC 本地 IP 地址之一:

private static String url_create_customer = "http//192.168.0.112/iChop/signup.php";

因为那是你服务器的IP。确保您的 Android 设备当然在同一个网络上。

于 2013-05-13T13:23:23.107 回答
1
//url to create new customer
private static String url_create_customer = "http//10.0.2.2/iChop/signup.php";

this URL is invalid as you are forgot the " : " !!!

于 2013-05-13T12:02:39.863 回答