无法连接到远程数据库

时间:2014-06-17 07:23:41

标签: android mysql json android-asynctask

我正在尝试连接到我自己的系统上托管的远程mySQL数据库,这意味着本地主机。我在这里粘贴了全班的代码,所以请看看它是否有错误。我面临的问题在类代码下面提到。 课程代码在这里:

public class Login extends Activity implements OnClickListener {

    private EditText user, pass;
    private Button mSubmit, mRegister;

    // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();
    private static final String LOGIN_URL="http://127.0.0.1:8080/webservice/login.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        // setup input fields
        user = (EditText) findViewById(R.id.username);
        pass = (EditText) findViewById(R.id.password);

        // setup buttons
        mSubmit = (Button) findViewById(R.id.login);
        mRegister = (Button) findViewById(R.id.register);

        // register listeners
        mSubmit.setOnClickListener(this);
        mRegister.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.login:
            new AttemptLogin().execute();
            break;
        case R.id.register:
            Intent i = new Intent(this, Register.class);
            startActivity(i);
            break;

        default:
            break;
        }
    }

    class AttemptLogin extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Login.this);
            pDialog.setMessage("Attempting login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;
            String username = user.getText().toString();
            String password = pass.getText().toString();
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));

                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
                        params);

                // check your log for json response
                Log.d("Login attempt", "login attempt");

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("Login Successful!", json.toString());
                    // save user data
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(Login.this);
                    Editor edit = sp.edit();
                    edit.putString("username", username);
                    edit.commit();

                    Intent i = new Intent(Login.this, ReadComments.class);
                    finish();
                    startActivity(i);
                    return json.getString(TAG_MESSAGE);
                } else {
                    Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null) {
                Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
            }

        }

    }

}

在显示“尝试登录”对话框并显示“数据库已关闭”消息后,应用程序崩溃。我在这里包含来自catlog的信息,所以如果它有帮助,请看看它。

06-17 03:19:18.309: D/dalvikvm(2019): GC_FOR_ALLOC freed 161K, 8% free 2971K/3224K, paused 35ms, total 49ms
06-17 03:19:18.429: D/request!(2019): starting
06-17 03:19:18.459: I/Choreographer(2019): Skipped 36 frames!  The application may be doing too much work on its main thread.
06-17 03:19:18.609: I/Choreographer(2019): Skipped 129 frames!  The application may be doing too much work on its main thread.
06-17 03:19:18.669: I/Choreographer(2019): Skipped 43 frames!  The application may be doing too much work on its main thread.
06-17 03:19:18.739: I/Choreographer(380): Skipped 61 frames!  The application may be doing too much work on its main thread.
06-17 03:19:18.819: I/Choreographer(380): Skipped 37 frames!  The application may be doing too much work on its main thread.
06-17 03:19:18.909: I/Choreographer(2019): Skipped 164 frames!  The application may be doing too much work on its main thread.
06-17 03:19:18.929: I/Choreographer(380): Skipped 36 frames!  The application may be doing too much work on its main thread.
06-17 03:19:18.939: I/Choreographer(2019): Skipped 33 frames!  The application may be doing too much work on its main thread.
06-17 03:19:19.039: I/Choreographer(2019): Skipped 30 frames!  The application may be doing too much work on its main thread.
06-17 03:19:19.299: I/Choreographer(2019): Skipped 32 frames!  The application may be doing too much work on its main thread.
06-17 03:19:19.399: I/Choreographer(2019): Skipped 56 frames!  The application may be doing too much work on its main thread.
06-17 03:19:20.819: I/Choreographer(2019): Skipped 42 frames!  The application may be doing too much work on its main thread.
06-17 03:19:20.889: I/Choreographer(2019): Skipped 32 frames!  The application may be doing too much work on its main thread.
06-17 03:19:21.749: I/Choreographer(2019): Skipped 37 frames!  The application may be doing too much work on its main thread.
06-17 03:19:21.879: I/Choreographer(2019): Skipped 48 frames!  The application may be doing too much work on its main thread.
06-17 03:19:22.449: I/Choreographer(2019): Skipped 52 frames!  The application may be doing too much work on its main thread.
06-17 03:19:22.749: W/System.err(2019): org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1:8080 refused
06-17 03:19:22.779: I/Choreographer(2019): Skipped 55 frames!  The application may be doing too much work on its main thread.
06-17 03:19:22.989: W/System.err(2019):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
06-17 03:19:22.989: W/System.err(2019):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-17 03:19:22.989: W/System.err(2019):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-17 03:19:23.029: I/Choreographer(2019): Skipped 43 frames!  The application may be doing too much work on its main thread.
06-17 03:19:23.279: W/System.err(2019):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-17 03:19:23.279: W/System.err(2019):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-17 03:19:23.279: W/System.err(2019):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-17 03:19:23.279: W/System.err(2019):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-17 03:19:23.289: W/System.err(2019):     at com.example.mysqltest.JSONParser.makeHttpRequest(JSONParser.java:110)
06-17 03:19:23.289: W/System.err(2019):     at com.example.mysqltest.Login$AttemptLogin.doInBackground(Login.java:121)
06-17 03:19:23.289: W/System.err(2019):     at com.example.mysqltest.Login$AttemptLogin.doInBackground(Login.java:1)
06-17 03:19:23.289: W/System.err(2019):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-17 03:19:23.289: W/System.err(2019):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-17 03:19:23.289: W/System.err(2019):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-17 03:19:23.289: W/System.err(2019):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-17 03:19:23.289: W/System.err(2019):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-17 03:19:23.289: W/System.err(2019):     at java.lang.Thread.run(Thread.java:841)
06-17 03:19:23.319: I/Choreographer(2019): Skipped 42 frames!  The application may be doing too much work on its main thread.
06-17 03:19:23.739: W/System.err(2019): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 8080): connect failed: ECONNREFUSED (Connection refused)
06-17 03:19:23.739: W/System.err(2019):     at libcore.io.IoBridge.connect(IoBridge.java:114)
06-17 03:19:23.739: W/System.err(2019):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-17 03:19:23.759: W/System.err(2019):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-17 03:19:23.769: W/System.err(2019):     at java.net.Socket.connect(Socket.java:843)
06-17 03:19:23.769: W/System.err(2019):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-17 03:19:23.769: W/System.err(2019):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-17 03:19:23.769: W/System.err(2019):     ... 15 more
06-17 03:19:23.769: W/System.err(2019): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
06-17 03:19:23.829: I/Choreographer(2019): Skipped 84 frames!  The application may be doing too much work on its main thread.
06-17 03:19:23.839: W/System.err(2019):     at libcore.io.Posix.connect(Native Method)
06-17 03:19:23.839: W/System.err(2019):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
06-17 03:19:23.849: W/System.err(2019):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-17 03:19:23.849: W/System.err(2019):     at libcore.io.IoBridge.connect(IoBridge.java:112)
06-17 03:19:23.849: W/System.err(2019):     ... 20 more
06-17 03:19:23.849: E/Buffer Error(2019): Error converting result java.lang.NullPointerException: lock == null
06-17 03:19:23.879: I/Choreographer(2019): Skipped 49 frames!  The application may be doing too much work on its main thread.
06-17 03:19:24.439: I/Choreographer(2019): Skipped 31 frames!  The application may be doing too much work on its main thread.
06-17 03:19:24.499: I/Choreographer(2019): Skipped 34 frames!  The application may be doing too much work on its main thread.
06-17 03:19:24.569: E/JSON Parser(2019): Error parsing data org.json.JSONException: End of input at character 0 of 
06-17 03:19:24.569: D/Login attempt(2019): login attempt
06-17 03:19:25.019: W/dalvikvm(2019): threadid=11: thread exiting with uncaught exception (group=0xb4a8aba8)
06-17 03:19:25.199: E/AndroidRuntime(2019): FATAL EXCEPTION: AsyncTask #1
06-17 03:19:25.199: E/AndroidRuntime(2019): Process: com.example.mysqltest, PID: 2019
06-17 03:19:25.199: E/AndroidRuntime(2019): java.lang.RuntimeException: An error occured while executing doInBackground()
06-17 03:19:25.199: E/AndroidRuntime(2019):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
06-17 03:19:25.199: E/AndroidRuntime(2019):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
06-17 03:19:25.199: E/AndroidRuntime(2019):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
06-17 03:19:25.199: E/AndroidRuntime(2019):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
06-17 03:19:25.199: E/AndroidRuntime(2019):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-17 03:19:25.199: E/AndroidRuntime(2019):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-17 03:19:25.199: E/AndroidRuntime(2019):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-17 03:19:25.199: E/AndroidRuntime(2019):     at java.lang.Thread.run(Thread.java:841)
06-17 03:19:25.199: E/AndroidRuntime(2019): Caused by: java.lang.NullPointerException
06-17 03:19:25.199: E/AndroidRuntime(2019):     at com.example.mysqltest.Login$AttemptLogin.doInBackground(Login.java:128)
06-17 03:19:25.199: E/AndroidRuntime(2019):     at com.example.mysqltest.Login$AttemptLogin.doInBackground(Login.java:1)
06-17 03:19:25.199: E/AndroidRuntime(2019):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-17 03:19:25.199: E/AndroidRuntime(2019):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-17 03:19:25.199: E/AndroidRuntime(2019):     ... 4 more
06-17 03:19:25.229: I/Choreographer(2019): Skipped 35 frames!  The application may be doing too much work on its main thread.
06-17 03:19:25.279: W/ActivityManager(380):   Force finishing activity com.example.mysqltest/.Login
06-17 03:19:25.309: D/gralloc(51): Registering a buffer in the process that created it. This may cause memory ordering problems.
06-17 03:19:25.309: E/libEGL(51): called unimplemented OpenGL ES API
06-17 03:19:25.309: E/libEGL(51): called unimplemented OpenGL ES API
06-17 03:19:25.319: E/libEGL(51): called unimplemented OpenGL ES API
06-17 03:19:25.319: E/libEGL(51): called unimplemented OpenGL ES API
06-17 03:19:25.319: E/SurfaceFlinger(51): glCheckFramebufferStatusOES error 886920962
06-17 03:19:25.319: E/SurfaceFlinger(51): got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot
06-17 03:19:25.319: E/libEGL(51): called unimplemented OpenGL ES API
06-17 03:19:25.319: E/libEGL(51): called unimplemented OpenGL ES API
06-17 03:19:25.329: I/Choreographer(2019): Skipped 91 frames!  The application may be doing too much work on its main thread.
06-17 03:19:25.359: W/WindowManager(380): Screenshot failure taking screenshot for (123x221) to layer 21015
06-17 03:19:25.589: I/Choreographer(380): Skipped 92 frames!  The application may be doing too much work on its main thread.
06-17 03:19:25.599: I/Choreographer(2019): Skipped 203 frames!  The application may be doing too much work on its main thread.
06-17 03:19:25.669: I/Choreographer(2019): Skipped 65 frames!  The application may be doing too much work on its main thread.
06-17 03:19:25.719: I/Choreographer(380): Skipped 115 frames!  The application may be doing too much work on its main thread.
06-17 03:19:25.779: I/Choreographer(2019): Skipped 92 frames!  The application may be doing too much work on its main thread.
06-17 03:19:25.839: I/Choreographer(542): Skipped 288 frames!  The application may be doing too much work on its main thread.
06-17 03:19:25.869: I/Choreographer(380): Skipped 35 frames!  The application may be doing too much work on its main thread.
06-17 03:19:25.939: I/Choreographer(380): Skipped 45 frames!  The application may be doing too much work on its main thread.
06-17 03:19:25.939: I/Choreographer(2019): Skipped 41 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.069: I/Choreographer(380): Skipped 124 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.119: I/Choreographer(380): Skipped 41 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.129: I/Choreographer(2019): Skipped 35 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.169: I/Choreographer(2019): Skipped 30 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.259: I/Choreographer(380): Skipped 35 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.269: I/Choreographer(2019): Skipped 89 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.309: I/Choreographer(380): Skipped 35 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.319: I/Choreographer(2019): Skipped 46 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.369: I/Choreographer(380): Skipped 46 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.379: I/Choreographer(2019): Skipped 54 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.469: I/Choreographer(2019): Skipped 80 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.499: I/Choreographer(380): Skipped 115 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.589: I/Choreographer(2019): Skipped 116 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.619: I/Choreographer(380): Skipped 80 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.689: I/Choreographer(380): Skipped 37 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.849: I/Choreographer(380): Skipped 77 frames!  The application may be doing too much work on its main thread.
06-17 03:19:26.939: I/Choreographer(380): Skipped 43 frames!  The application may be doing too much work on its main thread.
06-17 03:19:27.009: I/Choreographer(380): Skipped 41 frames!  The application may be doing too much work on its main thread.
06-17 03:19:27.169: I/Choreographer(380): Skipped 36 frames!  The application may be doing too much work on its main thread.
06-17 03:19:27.349: E/WindowManager(2019): android.view.WindowLeaked: Activity com.example.mysqltest.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b4d95578 V.E..... R.....ID 0,0-228,72} that was originally added here
06-17 03:19:27.349: E/WindowManager(2019):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
06-17 03:19:27.349: E/WindowManager(2019):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
06-17 03:19:27.349: E/WindowManager(2019):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
06-17 03:19:27.349: E/WindowManager(2019):  at android.app.Dialog.show(Dialog.java:286)
06-17 03:19:27.349: E/WindowManager(2019):  at com.example.mysqltest.Login$AttemptLogin.onPreExecute(Login.java:103)
06-17 03:19:27.349: E/WindowManager(2019):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
06-17 03:19:27.349: E/WindowManager(2019):  at android.os.AsyncTask.execute(AsyncTask.java:535)
06-17 03:19:27.349: E/WindowManager(2019):  at com.example.mysqltest.Login.onClick(Login.java:82)
06-17 03:19:27.349: E/WindowManager(2019):  at android.view.View.performClick(View.java:4438)
06-17 03:19:27.349: E/WindowManager(2019):  at android.view.View$PerformClick.run(View.java:18422)
06-17 03:19:27.349: E/WindowManager(2019):  at android.os.Handler.handleCallback(Handler.java:733)
06-17 03:19:27.349: E/WindowManager(2019):  at android.os.Handler.dispatchMessage(Handler.java:95)
06-17 03:19:27.349: E/WindowManager(2019):  at android.os.Looper.loop(Looper.java:136)
06-17 03:19:27.349: E/WindowManager(2019):  at android.app.ActivityThread.main(ActivityThread.java:5017)
06-17 03:19:27.349: E/WindowManager(2019):  at java.lang.reflect.Method.invokeNative(Native Method)
06-17 03:19:27.349: E/WindowManager(2019):  at java.lang.reflect.Method.invoke(Method.java:515)
06-17 03:19:27.349: E/WindowManager(2019):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-17 03:19:27.349: E/WindowManager(2019):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-17 03:19:27.349: E/WindowManager(2019):  at dalvik.system.NativeStart.main(Native Method)
06-17 03:19:27.359: I/Choreographer(2019): Skipped 273 frames!  The application may be doing too much work on its main thread.
06-17 03:19:27.449: I/Choreographer(2019): Skipped 85 frames!  The application may be doing too much work on its main thread.
06-17 03:20:10.919: D/LightsService(380): Excessive delay setting light: 89ms

请注意,关于catlog信息是在我按下登录按钮之后。

1 个答案:

答案 0 :(得分:0)

Please, check mysql-service is running or not.
If no then service is stop, then check space available at harddisk.
If space is not availbale then try to increase free space or increase 
hard disk space, then restart mysql service.