连接使用Android模拟器远程系统的IP地址,远程MySQL数据库 [英] connecting remote mysql database using IP address of the remote system in android emulator

查看:250
本文介绍了连接使用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.

这篇关于连接使用Android模拟器远程系统的IP地址,远程MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆