尝试连接到MySql时出错

时间:2015-04-14 22:18:56

标签: android

我正在尝试将mysql连接到Android应用程序,但它会崩溃应用程序。

这是代码:

    public void registerUser( final String name,
             final String password,  final String answer,  final String question) {
        // Tag used to cancel the request
        String tag_string_req = "req_register";

        pDialog.setMessage("Registering ...");
        showDialog();

        StringRequest strReq = new StringRequest(Method.POST,
                AppConfig.URL_REGISTER, new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, "Register Response: " + response.toString());
                        hideDialog();

                        try {
                            JSONObject jObj = new JSONObject(response);
                            boolean error = jObj.getBoolean("error");
                            if (!error) {
                                // User successfully stored in MySQL
                                // Now store the user in sqlite

                                JSONObject user = jObj.getJSONObject("user");
                                String name = user.getString("name");
                                String password = user.getString("password");
                                String quesion = user.getString("question");
                                String answer = user.getString("answer");
                                String created_at = user
                                        .getString("created_at");

                                // Inserting row in users table
                                db.addUser(name,password,question,answer, created_at);

                                // Launch login activity
                                Intent intent = new Intent(
                                        Sign__up.this,
                                        Sign_in.class);
                                startActivity(intent);
                                finish();
                            } else {

                                // Error occurred in registration. Get the error
                                // message
                                String errorMsg = jObj.getString("error_msg");
                                Toast.makeText(getApplicationContext(),
                                        errorMsg, Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e(TAG, "Registration Error: " + error.getMessage());
                        Toast.makeText(getApplicationContext(),
                                error.getMessage(), Toast.LENGTH_LONG).show();
                        hideDialog();
                    }
                }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting params to register url
                Map<String, String> params = new HashMap<String, String>();
                params.put("tag", "register");
                params.put("name", name);
                params.put("password", password);
                params.put("question", question);
                params.put("answer", answer);

                return params;
            }

        };

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

    private void showDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hideDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

}

这是Log_Cat

04-14 23:59:53.923: E/AndroidRuntime(4974): FATAL EXCEPTION: main
04-14 23:59:53.923: E/AndroidRuntime(4974): java.lang.NullPointerException
04-14 23:59:53.923: E/AndroidRuntime(4974):     at com.example.atw.Sign__up.registerUser(Sign__up.java:202)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at com.example.atw.Sign__up$1.onClick(Sign__up.java:102)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at android.view.View.performClick(View.java:4084)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at android.view.View$PerformClick.run(View.java:16989)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at android.os.Handler.handleCallback(Handler.java:615)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at android.os.Looper.loop(Looper.java:137)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at android.app.ActivityThread.main(ActivityThread.java:4813)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at java.lang.reflect.Method.invoke(Method.java:511)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案