2012-03-27 179 views
1

我試圖用KSOAP2調用.NET Web服務,它在androidHttpTransport.call(SOAP_ACTION, envelope)和因此return result="m"引發異常。從Android與KSOAP2調用.NET Web服務

我該怎麼做才能調用函數?

這裏是我的代碼:

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


     }); 

    } 


} 

謝謝!

回答

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()); 
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"; 
+0

感謝您的幫助!我找到了解決方案。 :d – user1296060 2012-03-28 03:26:40