与异步任务连接到PostgreSQL崩溃

时间:2013-02-15 09:51:54

标签: android postgresql android-asynctask database-connection

使用ASynchTask连接到PostgreSQL数据库时出现问题。 使用正确的数据库设置(pg_host,pg_user等)进行尝试时,它可以正常工作。问题是当使用错误设置尝试它时,Activity将关闭,因为getConnection()方法具有false参数。 但是我希望用户手动将设置放在settings_activity中,这样他就可以更改自己数据库的设置,如果存储了错误的设置,则会出现警告toast并提示用户更改设置。 每次活动都会中断,我做错了什么? 非常感谢。

CODE:

1 protected void onCreate(Bundle savedInstanceState) {
2    super.onCreate(savedInstanceState);
3    setContentView(R.layout.main);
4   
5    getPrefs();
6     //.
7     //.
8     //.
9  new FetchSQL().execute();
10 }    
11
12 public void getPrefs() {
13   SharedPreferences prefs = PreferenceManager
14   .getDefaultSharedPreferences(this);
15
16   pg_host = prefs.getString("host", "HOST");
17   pg_port = prefs.getString("port", "PORT");
18   pg_database = prefs.getString("database", "DATABASE");
19   pg_user = prefs.getString("user", "USER");
20   pg_pw = prefs.getString("password", "PW");
21 }
22
23 
24  private class FetchSQL extends AsyncTask<Void,Void,String> {
25
26   @Override
27   protected String doInBackground(Void... params) {
28   String retval="";
29   try {
30   Class.forName("org.postgresql.Driver");
31   } catch (ClassNotFoundException e) {
32   e.printStackTrace();
33   retval = e.toString();
34   Toast.makeText(MainActivity.this,e.toString(), Toast.LENGTH_LONG).show();
35   }
36   String url =   "jdbc:postgresql://"+pg_host+":"+pg_port+"/"+pg_database+"";
37   Connection conn;
38   try {
39   DriverManager.setLoginTimeout(5);
40   conn = DriverManager.getConnection(url, pg_user, pg_pw);
41   Statement st = conn.createStatement();
42   String sql;
43   sql = //SQL Query
44   ResultSet rs = st.executeQuery(sql);
45   while(rs.next()) {
46 
47   //e.g var .. = rs.getString("...");
48   }
49   rs.close();
50   st.close();
51   conn.close();
52   }
53   catch (SQLException e) {
54   e.printStackTrace();
55   retval = e.toString();
56   Toast.makeText(MainActivity.this,e.toString(), Toast.LENGTH_LONG).show();
57   }
58   return retval;
59
60   }
61  
62   @Override 
63   protected void onPostExecute(String value) {
64 //Show Data from Query in textView
65 }

logcat的:

102-14 12:21:18.707: W/System.err(2128): java.sql.SQLException: No suitable driver
202-14 12:21:18.767: W/System.err(2128): at                   java.sql.DriverManager.getConnection(DriverManager.java:182)
302-14 12:21:18.877: W/System.err(2128): at java.sql.DriverManager.getConnection(DriverManager.java:209)
402-14 12:21:18.928: W/System.err(2128): at MainActivity$FetchSQL.doInBackground(MainActivity.java:318)
502-14 12:21:18.987: W/System.err(2128): at .MainActivity$FetchSQL.doInBackground(MainActivity.java:1)
602-14 12:21:19.047: W/System.err(2128): at android.os.AsyncTask$2.call(AsyncTask.java:287)
702-14 12:21:19.099: W/System.err(2128): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
802-14 12:21:19.157: W/System.err(2128): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
902-14 12:21:19.217: W/System.err(2128): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
1002-14 12:21:19.267: W/System.err(2128): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
1102-14 12:21:19.437: W/System.err(2128): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
1202-14 12:21:19.467: W/System.err(2128): at java.lang.Thread.run(Thread.java:856)

1 个答案:

答案 0 :(得分:0)

我认为问题是在后台线程上使用Toast。 尝试使用runOnUiThread(new Runnable()):

runOnUiThread(new Runnable() {
    @Override
    public void run() {
    Toast.makeText(MainActivity.this,e.toString(), Toast.LENGTH_LONG).show();
    }
});