Search code examples
androidweb-servicesksoap2

Calling .NET web service from android with KSOAP2


I tried to call a .NET web service with KSOAP2 and it throws an exception at androidHttpTransport.call(SOAP_ACTION, envelope) and thus return result="m".

What should I do so that it will call the function?

Here is my code:

public class LoginActivity extends Activity {

private static final String NAMESPACE = "http://tempuri.org/" ;
private static final String METHOD_NAME = "login";
private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
private static final String URL = "http://192.168.8.2/CompiledWebSite/WebService.asmx";


private EditText et_id,et_pwd;
private Button btn_login;

public String login(String str_id, String str_pwd){
    String result ="a";
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("s_id", str_id);
    request.addProperty("pw", str_pwd);

    SoapSerializationEnvelope envelope =
    new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet =  true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try
    {

    androidHttpTransport.call(SOAP_ACTION, envelope);

    SoapPrimitive Result = (SoapPrimitive)envelope.getResponse();

    result = Result.toString();
    }
    catch(Exception e) 
    {
        result="m";
    }
    return result;          
}

 @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn_login = (Button) findViewById(R.id.BnLogin);
        et_id = (EditText) findViewById(R.id.ETStudentID);
        et_pwd = (EditText) findViewById(R.id.ETPassword);

        btn_login.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v){

                String str_id, str_pwd;
                str_id = et_id.getText().toString();
                str_pwd = et_pwd.getText().toString();

                if (login(str_id,str_pwd)=="true"){
                     Toast toast=Toast.makeText(LoginActivity.this, str_id, 500);
                     toast.setGravity(1, 1, 1);

                     toast.show();
                }else{
                     Toast toast=Toast.makeText(LoginActivity.this, login(str_id,str_pwd), 500);
                     toast.setGravity(1, 1, 1);

                     toast.show();
                }
            }


        });

    }


 }

Thanks!


Solution

  • I found the solution. I add following code to AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
    

    then add following code to onCreate()

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()     
    .detectDiskReads()     
    .detectDiskWrites()     
    .detectNetwork()   // or .detectAll() for all detectable problems     
    .penaltyLog()     
    .build());     
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()     
    .detectLeakedSqlLiteObjects()     
    .detectLeakedClosableObjects()     
    .penaltyLog()     
    .penaltyDeath()     
    .build());