Android网络超时在真实设备上,但不在模拟器上

时间:2015-12-03 15:16:51

标签: android database networking jdbc android-emulator

就像标题所说,我有一个Android应用程序,其中用户通过数据库查询通过JDBC登录到远程功能数据库。登录模拟器,显然是朋友的Nvidia Shield,但不在我的Galaxy S4或我的Nexus 7上。我设置了我的互联网权限标志,服务器运行良好。

我的代码如下:

        new Handler().postDelayed(
            new Runnable() {
                public void run() {
                    login(email,password);
                    while (user()==null) {}
                    user = user();
                    if (user.equals("")) {
                        failed();
                    } else {
                        success();
                    }
                }
            }, 3000);

public void login (String email, String password) {
    try {
        Connection c = getcon();
        PreparedStatement prep = c.prepareStatement("SELECT user_id FROM [database.table] WHERE Email = ? AND password = ? LIMIT 1");
        prep.setString(1, email);
        prep.setString(2, hash(password));
        new login().execute(prep);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private class login extends AsyncTask<PreparedStatement, Void, String> {
    protected String doInBackground(PreparedStatement... params) {
        try {
            ResultSet rs = params[0].executeQuery();
            if (rs.next()) {
                Log.i("Utilities", "Login Successful");
                return rs.getString(1);
            }
            Log.i("Utilities", "Login Failed!");
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(String result) {
        if (result==null) {
            user = "";
        } else {
            user = result;
        }
    }
}

public Connection getcon() {
    try {
        Connection c = new getconn().execute().get();
        return c;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

private class getconn extends AsyncTask<Void, Void, Connection> {
    protected Connection doInBackground(Void... params) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            return DriverManager.getConnection("jdbc:mysql://dallas113.arvixeshared.com:3306" + "?useUnicode=true&characterEncoding=utf-8","[username]","[password]");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

有谁可以指出可能出现的问题?我得到的错误是线路上的连接超时 DriverManager.getConnection("jdbc:mysql://dallas113.arvixeshared.com:3306" + "?useUnicode=true&characterEncoding=utf-8","[username]","[password]");。另外,如果我发生了其他任何错误,请告诉我,我是Android新手。

0 个答案:

没有答案