连接使用Android模拟器远程系统的IP地址,远程MySQL数据库
问题描述:
这是下面的code:
`package com.tom.jam;
//import java.sql.Connection;
//import java.sql.DriverManager;
import java.sql.*;
import android.app.Activity;
//import android.database.SQLException;
import android.os.Bundle;
import android.widget.TextView;
public class MyserverActivity extends Activity {
TextView mf,ct;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mf=(TextView) findViewById(R.id.myfield);
ct=(TextView) findViewById(R.id.cont);
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://192.168.0.100/test","root", "secret");
if(!con.isClosed())
{
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from ram");
while(rs.next())
{
ct.setText("ID" + rs.getString(1) + "Name " + rs.getString(2));
}
mf.setText("Successfully connected to " +
"MySQL server using TCP/IP...");
}
} catch(Exception e) {
mf.setText("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
}catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}`
我们在
manifest:android.permission.ACCESS_NETWORK_STATE,
android.permission.INTERNET,
android.permission.WRITE_EXTERNAL_STORAGE
没有错误和警告,但在运行仿真器显示以下异常:
NO errors and warnings but while running emulator is showing the following exception:
Unable to connect to any hosts due to exception:java.net.SocketTimeoutException:Connection timed out.
在此先感谢
答
java.net.SocketTimeoutException:连接超时,当目标系统是不可达的异常发生。这可能是由于对你的模拟器没有设置代理服务器,设置代理的模拟器可以通过在命令提示符下命令来启动模拟器:
java.net.SocketTimeoutException:Connection timed out, exception occurs when destination system is not reachable. It may be due to proxy not set on your emulator, to set proxy on emulator you can start emulator by command in command prompt:
emulator -avd avdname -http-proxy http://192.168.1.1:8080
下面替换avdname您要运行程序AVD,和HTTP名称:192.168.1.1到您的代理服务器
Here replace avdname to name of avd you want to run your program, and http:192.168.1.1 to your proxy server.