Android 在IntentService中等待结果

Android 在IntentService中等待结果,android,android-intent,intentservice,Android,Android Intent,Intentservice,在使用IntentService时,是否有方法等待结果 情景 服务应用程序正在运行-->从另一个应用程序接收登录凭据-->服务应用程序然后检查数据库(由于某些原因,我总是会出现连接超时。我知道这是不好的,但对于POC和快速黑客来说,现在就可以了)-->等待验证查询,然后查看结果 这可能吗 我尝试在服务中使用AsynTask,但仍然没有用,我总是收到连接超时错误 人口服务 @Override public IBinder onBind(Intent intent) { return ne

在使用IntentService时,是否有方法等待结果

情景

服务应用程序正在运行-->从另一个应用程序接收登录凭据-->服务应用程序然后检查数据库(由于某些原因,我总是会出现连接超时。我知道这是不好的,但对于POC和快速黑客来说,现在就可以了)-->等待验证查询,然后查看结果

这可能吗

我尝试在服务中使用AsynTask,但仍然没有用,我总是收到连接超时错误

人口服务

@Override
public IBinder onBind(Intent intent) {

    return new IDemoService.Stub() {

        @Override
        /**
         * Login validation implementation
         */
        public boolean login(String id, String password)
                throws RemoteException {
            UserLoginTask userLoginTask = new UserLoginTask(id, password);
            try {
                return userLoginTask.execute((Void) null).get();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }
    };
}
用户登录任务

 private class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
    private String uId;
    private String uPassword;

    public UserLoginTask(String id, String password) {
        uId = id;
        uPassword = password;
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
        Connection conn;
        try {
            int count = 0;
            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://xxx.xxx.x.xxx/test_db?user=postgres&password=password";
            conn = DriverManager.getConnection(url);
            if (conn != null) {
                Statement st = conn.createStatement();
                String sql = "Select count(*) as cnt from tbl_persons where id = '"
                        + uId.toUpperCase(Locale.getDefault()).trim()
                        + "' AND pwd='" + uPassword.trim() + "';";

                Log.d("DemoService", "Login sql - " + sql);
                ResultSet rs = st.executeQuery(sql);
                while (rs.next()) {
                    count = rs.getInt("cnt");
                }

                rs.close();
                st.close();
                conn.close();

                if (count == 0)
                    return false;

                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

}
私有类UserLoginTask扩展异步任务{
私有字符串uId;
私有字符串uPassword;
公共用户登录任务(字符串id、字符串密码){
uId=id;
uPassword=密码;
}
@凌驾
受保护的布尔doInBackground(无效…arg0){
连接接头;
试一试{
整数计数=0;
Class.forName(“org.postgresql.Driver”);
String url=“jdbc:postgresql://xxx.xxx.x.xxx/test_db?user=postgres&password=password";
conn=DriverManager.getConnection(url);
如果(conn!=null){
语句st=conn.createStatement();
String sql=“选择count(*)作为来自tbl_persons的cnt,其中id=”“
+uId.toUpperCase(Locale.getDefault()).trim()
+“'和pwd='”+uPassword.trim()+“;”;
Log.d(“DemoService”、“登录sql-”+sql);
结果集rs=st.executeQuery(sql);
while(rs.next()){
计数=rs.getInt(“cnt”);
}
rs.close();
圣克洛斯();
康涅狄格州关闭();
如果(计数=0)
返回false;
返回true;
}
}捕获(SQLE异常){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
返回false;
}
}

你的问题不是很清楚,你能帮我发一些代码吗。我添加了一个示例代码。每次发生getConnection(url)时,我都会收到一个错误。我试图将ConnectionTime设置为Out,但仍然存在错误。我正在查看IntentService,但我不知道它是否可以等待正确的结果。您如何在android中使用postgresql,您应该使用SQLite我知道。但是一个系统已经就位,我所要做的就是验证该表。没有ws或rest服务器。问题似乎是“为什么连接超时”,而不是“在IntentService中等待结果”。其中不需要AsyncTask,因为IntentService已经“使用工作线程依次处理每个意图,并在工作结束时自行停止”。