【问题标题】:SocketTimeoutException while connecting to local database连接到本地数据库时出现 SocketTimeoutException
【发布时间】:2015-12-01 08:38:30
【问题描述】:

我正在尝试使用 DOT NET 网络服务将我的 android 应用程序连接到我的计算机中存在的 MySql 数据库 (localhost)。我可以通过单一输入(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");                    
                         }
				
	        }
}

【问题讨论】:

  • 您的设备在本地网络中像服务器吗?
  • 是的,手机和电脑都有相同的 WiFi 网络,我已将我的无线 IPv4 设为静态(与我的电脑 IP 地址相同)。
  • 关闭防火墙。
  • '将我的无线 IPv4 设为静态(与我的计算机 IP 地址相同)'。不明白你在这里做了什么。但看起来很可疑。不应该有与你的电脑相同 ip 的东西。
  • @greenapps。防火墙已关闭。通过静态 IP 方式,为了将我的应用程序连接到 Web 服务,我安装了 IIS 并启动了一个名为“Service”的虚拟网站,并将 Web 服务添加到该网站。将应用程序链接到 LoginService.asmx Web 服务的 SOAP ADDRESS 是从 localhost 完成的,其 IP 地址和端口号为 192.168.1.5 和 80。

标签: android web-services localhost


【解决方案1】:

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

【讨论】:

  • 做了同样的事情,但仍然得到相同的结果
猜你喜欢
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多