5

I am trying to connect my android app to MySql database (localhost) present in my computer using DOT NET webservice. I am able to connect to online database with only single input (EditText). Here there are 3 EditText input parameters and the database is present in computer only. The webservice receives the 3 parameters and checks if the 3 inputs are same as in the database. If YES, it returns a value and if NO it returns another value. I am just trying to save the returned value (response) to 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");                    
                         }
    
         }
}

Error

Somnath Pal
  • 1,570
  • 4
  • 23
  • 42
  • Is your device in local network like server? – dieter_h Sep 05 '15 at 07:49
  • Yes, both mobile and computer have the same WiFi network and I have made my Wireless IPv4 static (same as my computer IP address). – Somnath Pal Sep 05 '15 at 07:51
  • Switch off the firewall. – greenapps Sep 05 '15 at 09:01
  • 'made my Wireless IPv4 static (same as my computer IP address)'. Don't understand what you did here. But it looks suspicious. There should not be someting with the same ip as your pc. – greenapps Sep 05 '15 at 09:03
  • @greenapps. Firewall is turned off. By static IP means, to connect my app to webservice I installed IIS and started a dummy website named "Service" and added the webservice to the website. The SOAP ADDRESS linking the app to the LoginService.asmx webservice is done from localhost which has the IP address and port number 192.168.1.5 and 80. – Somnath Pal Sep 05 '15 at 09:10
  • Try that url in a browser on your android device. Does it connect? – greenapps Sep 05 '15 at 10:14
  • Well then you know what you have to do first. Better the setup and check with your browsers before you try the android app again. – greenapps Sep 05 '15 at 10:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88873/discussion-between-somnath-pal-and-greenapps). – Somnath Pal Sep 05 '15 at 11:00

1 Answers1

1

You must sure your MySQL Server allowed to connect by public MySQL client. To config it try see here.

Community
  • 1
  • 1