使用KSOAP2从android调用.NET Web服务

时间:2012-03-27 16:21:08

标签: android web-services ksoap2

我尝试使用KSOAP2调用.NET Web服务,并在androidHttpTransport.call(SOAP_ACTION, envelope)return result="m"处抛出异常。

我应该怎么做才能调用这个函数?

这是我的代码:

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();
                }
            }


        });

    }


 }

谢谢!

2 个答案:

答案 0 :(得分:1)

我找到了解决方案。 我将以下代码添加到AndroidManifest.xml

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

然后将以下代码添加到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());

答案 1 :(得分:0)

替换此行:

private static final String URL = "http://192.168.8.2/CompiledWebSite/WebService.asmx

这一个:

private static final String URL = "http://192.168.8.2/CompiledWebSite/WebService.asmx?wsdl";