草庐IT

android - 连接到本地数据库时出现 SocketTimeoutException

coder 2023-12-16 原文

我正在尝试使用 DOT NET 网络服务将我的 Android 应用程序连接到我计算机中存在的 MySql 数据库(本地主机)。我能够仅通过单一输入 (EditText) 连接到在线数据库。这里有 3 个 EditText 输入参数,数据库仅存在于计算机中。 Web 服务接收 3 个参数并检查这 3 个输入是否与数据库中的相同。如果是,则返回一个值,如果不是,则返回另一个值。我只是想将返回值(响应)保存到 TextView。

LoginService.asmx

[WebService(Namespace = "Check_Activity")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class LoginService  
{
    MySqlConnection objCon = new MySqlConnection(ConfigurationManager.ConnectionStrings["ActiveConnection"].ConnectionString); 
    MySqlCommand objSqlCmd; 
    MySqlParameter objSqlParam;
    [WebMethod]
    public string LoginStatus(string uid, string pswrd, string category) 
    {
        string returndata = "";
        try
        {
            if (objCon.State != ConnectionState.Open)
            {
                objCon.Open();
            }
            objSqlCmd = new MySqlCommand("rawProcedure", objCon);
            objSqlCmd.CommandType = CommandType.StoredProcedure;
            objSqlCmd.Parameters.AddWithValue("UID", uid);
            objSqlCmd.Parameters.AddWithValue("PASS", pswrd);
            objSqlCmd.Parameters.AddWithValue("CAT", category);
            objSqlParam = new MySqlParameter();
            objSqlParam.ParameterName = "Response";
            objSqlParam.MySqlDbType = MySqlDbType.VarChar;
            objSqlParam.Direction = ParameterDirection.Output;
            objSqlCmd.Parameters.Add(objSqlParam);
            objSqlCmd.ExecuteNonQuery();
            objCon.Close();
            returndata = objSqlParam.Value.ToString();
            return returndata; ;
        }
        catch(Exception ex)
        {
            return returndata = "Exception";
        }
    }
}
activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="41dp"
        android:hint="User ID"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="50dp"
        android:hint="Password"
        android:ems="10" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="64dp"
        android:hint="Category"
        android:ems="10" />

    

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editText3"
        android:layout_marginTop="20dp"
        android:onClick="RUN"
        android:text="Get It !" />
    
     <TextView
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" />

</RelativeLayout>
MainActivity

public class MainActivity extends Activity {
private static final String SOAP_ACTION = "WSDL_TARGET_NAMESPACE + METHOD";
    
    private static final String OPERATION_NAME = "LoginStatus";// your webservice web method name
    
    private static final String WSDL_TARGET_NAMESPACE = "Check_Activity";
    
    private static final String SOAP_ADDRESS = "http://192.168.1.5:80/LoginService.asmx";
	private TextView textView;
	EditText userId, pass, cat;
	String userId_str, pass_str, cat_str;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		if (android.os.Build.VERSION.SDK_INT > 9) {
    		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    		StrictMode.setThreadPolicy(policy);
    	}
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		textView = (TextView) findViewById(R.id.test);
		
		
		
	}	
		
			public void RUN(View v){
		        
				 SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,     OPERATION_NAME);
                 PropertyInfo propertyInfo1 = new PropertyInfo();
                 propertyInfo1.type = PropertyInfo.STRING_CLASS;
                 propertyInfo1.name = "userId_str";
                 
                 PropertyInfo propertyInfo2 = new PropertyInfo();
                 propertyInfo2.type = PropertyInfo.STRING_CLASS;
                 propertyInfo2.name = "pass_str";
                 
                 PropertyInfo propertyInfo3 = new PropertyInfo();
                 propertyInfo3.type = PropertyInfo.STRING_CLASS;
                 propertyInfo3.name = "cat_str";
                 
                 userId = (EditText) findViewById(R.id.editText1);
         		 pass = (EditText) findViewById(R.id.editText2);
         		 cat = (EditText) findViewById(R.id.editText3);
                 
         		 userId_str=userId.getText().toString();
         		 pass_str=pass.getText().toString();
         		 cat_str=cat.getText().toString();
                 
         		//request.addProperty(propertyInfo1, userId_str);
         		 request.addPropertyIfValue(propertyInfo1, userId_str);
         		 request.addPropertyIfValue(propertyInfo1, userId_str);
         		 request.addPropertyIfValue(propertyInfo1, userId_str);
                         
                         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                         SoapEnvelope.VER11);
                         envelope.dotNet = true;
                         
                         envelope.setOutputSoapObject(request);
                         
                         HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
                         
                         try  {                    
                         httpTransport.call(SOAP_ACTION, envelope);                    
                         Object response = envelope.getResponse();                    
                         textView.setText(response.toString());                    
                         }  catch (Exception exception)   {
                        	 textView.setText(exception.toString()+"  Or Invalid Entry");                    
                         }
				
	        }
}

最佳答案

您必须确保您的 MySQL 服务器允许通过公共(public) MySQL 客户端进行连接。 要配置它,请尝试 see here.

关于android - 连接到本地数据库时出现 SocketTimeoutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32410726/

有关android - 连接到本地数据库时出现 SocketTimeoutException的更多相关文章

  1. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  2. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  5. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  6. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  7. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  8. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  9. ruby - 是否可以覆盖 gemfile 进行本地开发? - 2

    我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI

  10. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

随机推荐