2015-09-05 2 views
5

Я пытаюсь подключить свое приложение Android к базе данных MySql (localhost), присутствующей на моем компьютере, используя веб-службу DOT NET. Я могу подключиться к онлайн-базе данных только с одним входом (EditText). Здесь есть 3 входных параметра EditText, и база данных присутствует только на компьютере. Вебсервис получает 3 параметра и проверяет, являются ли эти 3 входа такими же, как в базе данных. Если ДА, он возвращает значение, и если НЕТ, он возвращает другое значение. Я просто пытаюсь сохранить возвращаемое значение (ответ) в TextView.SocketTimeoutException при подключении к локальной базе данных

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"; 
 
\t private TextView textView; 
 
\t EditText userId, pass, cat; 
 
\t String userId_str, pass_str, cat_str; 
 

 
\t @Override 
 
\t protected void onCreate(Bundle savedInstanceState) { 
 
\t \t 
 
\t \t if (android.os.Build.VERSION.SDK_INT > 9) { 
 
    \t \t StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
 
    \t \t StrictMode.setThreadPolicy(policy); 
 
    \t } 
 
\t \t 
 
\t \t super.onCreate(savedInstanceState); 
 
\t \t setContentView(R.layout.activity_main); 
 
\t \t 
 
\t \t textView = (TextView) findViewById(R.id.test); 
 
\t \t 
 
\t \t 
 
\t \t 
 
\t } \t 
 
\t \t 
 
\t \t \t public void RUN(View v){ 
 
\t \t   
 
\t \t \t \t 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); 
 
      \t \t pass = (EditText) findViewById(R.id.editText2); 
 
      \t \t cat = (EditText) findViewById(R.id.editText3); 
 
        
 
      \t \t userId_str=userId.getText().toString(); 
 
      \t \t pass_str=pass.getText().toString(); 
 
      \t \t cat_str=cat.getText().toString(); 
 
        
 
      \t \t //request.addProperty(propertyInfo1, userId_str); 
 
      \t \t request.addPropertyIfValue(propertyInfo1, userId_str); 
 
      \t \t request.addPropertyIfValue(propertyInfo1, userId_str); 
 
      \t \t 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) { 
 
         \t textView.setText(exception.toString()+" Or Invalid Entry");      
 
         } 
 
\t \t \t \t 
 
\t   } 
 
}

Error

+0

Является ли ваше устройство в локальной сети, например сервером? –

+0

Да, как мобильная, так и компьютерная сеть имеют одну и ту же сеть Wi-Fi, и я сделал свой статический IPv4 для беспроводной сети (такой же, как и для моего IP-адреса компьютера). –

+0

Отключите брандмауэр. – greenapps

ответ

1

Вы должны, что ваш почтовый сервер MySQL разрешено подключаться общественным клиентом MySQL. Чтобы настроить его, попробуйте see here.

+0

Был ли тот же самый, но все же получил тот же результат –

Смежные вопросы