Android.os.on主线程异常



我试图连接到我的sql服务器上的sql DB,使用下面的代码。但是当我运行它时,显示了上面的错误。我已经将权限行添加到清单中,但仍然没有运气。

任何建议将不胜感激!

Log.i("Android"," MySQL Connect Example.");
Connection conn = null;
try {
    String driver = "net.sourceforge.jtds.jdbc.Driver";
    Class.forName(driver).newInstance();
    //test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
    String connString = "jdbc:jtds:sqlserver://server_ip_address :1433/DBNAME;encrypt=fasle;user=xxxxxxxxx;password=xxxxxxxx;instance=SQLEXPRESS;";
    String username = "xxxxxx";
    String password = "xxxxxxxxxx";
    conn = DriverManager.getConnection(connString,username,password);
    Log.w("Connection","open");
    Statement stmt = conn.createStatement();
    ResultSet reset = stmt.executeQuery("select * from TableName");
    //Print the data to the console
    while(reset.next()){
        Log.w("Data:",reset.getString(3));
        //Log.w("Data",reset.getString(2));
    }
    conn.close();
} catch (Exception e) {
    Log.w("Error connection","" + e.getMessage());
}

NetworkOnMainThreadException:当应用程序试图在其主线程上执行网络操作时抛出的异常。

你应该在asynctask上调用sendfeedback方法,然后只有上面的代码才能工作。由于web服务器花费大量时间来响应,主线程变得无响应。为了避免它,您应该在另一个线程上调用它。因此AsyncTask更好。

http://android-developers.blogspot.in/2009/05/painless-threading.html

最新更新