2009-09-27 194 views
21

我有一些使用WSDL/SOAP進行通信的Web服務。具體來說,我使用PHP和Nusoap來製作它們。我如何在Android上使用這些Web服務?我很快就會獲得一款新​​的Android手機,所以我需要知道。Android WSDL/SOAP服務客戶端

用Windows Mobile和Visual Studio很容易做到這一點。

謝謝

回答

5

我創建了一個新的SOAP客戶端Android平臺它使用JAX-WS生成的接口,但它只是一個概念驗證。

如果你有興趣,請嘗試的例子和/或觀看來源:http://wiki.javaforum.hu/display/ANDROIDSOAP/Home

更新:版本0.0.4脫離與教程:

http://wiki.javaforum.hu/display/ANDROIDSOAP/2012/04/16/Version+0.0.4+released

http://wiki.javaforum.hu/display/ANDROIDSOAP/Step+by+step+tutorial

+0

不錯的一個。保持良好的工作! – DeliveryNinja 2010-10-19 15:37:31

1
我昨天發現的

Icesoap看起來很有希望。它在一個基本的測試上工作,但它缺少更多的例子。

+0

我在執行請求時收到空指針異常。可能是什麼原因? – Som 2012-05-31 11:32:34

9

我創辦這個工具自動生成WSDL到Android代碼,

http://www.wsdl2code.com/Example.aspx

public void callWebService(){ 
    SampleService srv1 = new SampleService(); 
    Request req = new Request(); 
    req.companyId = "1"; 
    req.userName = "userName"; 
    req.password = "pas"; 
    Response response = srv1.ServiceSample(req); 
} 
0
private static final String NAMESPACE = "http://tempuri.org/"; 
private static final String URL = "http://example.com/CRM/Service.svc"; 
private static final String SOAP_ACTION = "http://tempuri.org/Login"; 
private static final String METHOD_NAME = "Login"; 


//calling web services method 

String loginresult=callService(username,password,usertype); 


//calling webservices 
String callService(String a1,String b1,Integer c1) throws Exception { 

      Boolean flag=true; 
      do 
      { 
       try{ 
        System.out.println(flag); 

       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

       PropertyInfo pa1 = new PropertyInfo(); 
       pa1.setName("Username"); 
       pa1.setValue(a1.toString()); 

       PropertyInfo pb1 = new PropertyInfo(); 
       pb1.setName("Password"); 
       pb1.setValue(b1.toString()); 

       PropertyInfo pc1 = new PropertyInfo(); 
       pc1.setName("UserType"); 
       pc1.setValue(c1); 
       System.out.println(c1+"this is integer****s"); 




       System.out.println("new"); 
       request.addProperty(pa1); 
       request.addProperty(pb1); 
       request.addProperty(pc1); 



       System.out.println("new2"); 
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
       envelope.dotNet = true; 
       System.out.println("new3"); 
       envelope.setOutputSoapObject(request); 
       HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
       androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
       System.out.println("new4"); 
       try{ 
       androidHttpTransport.call(SOAP_ACTION, envelope); 
       } 
       catch(Exception e) 
       { 

        System.out.println(e+" this is exception"); 
       } 
       System.out.println("new5"); 
       SoapObject response = (SoapObject)envelope.bodyIn; 

       result = response.getProperty(0).toString(); 

       flag=false; 
       System.out.println(flag); 
       }catch (Exception e) { 
       // TODO: handle exception 
        flag=false; 
      } 
      } 
      while(flag); 
      return result; 

     } 
/// 
1

我剛剛完成了一個Android應用程序有關WSDL的,我有一些提示,以追加:

1.最重要的資源是www.wsdl2code.com

2。你可以把你的用戶名和密碼,用頭,這與Base64編碼,如:

 String USERNAME = "yourUsername"; 
    String PASSWORD = "yourPassWord"; 
    StringBuffer auth = new StringBuffer(USERNAME); 
    auth.append(':').append(PASSWORD); 
    byte[] raw = auth.toString().getBytes(); 
    auth.setLength(0); 
    auth.append("Basic "); 
    org.kobjects.base64.Base64.encode(raw, 0, raw.length, auth); 
    List<HeaderProperty> headers = new ArrayList<HeaderProperty>(); 
    headers.add(new HeaderProperty("Authorization", auth.toString())); // "Basic V1M6")); 

    Vectordianzhan response = bydWs.getDianzhans(headers); 

3.somethimes,你不知道或者ANDROID代碼或Web服務器是錯的,那麼調試 是重要。在樣品,捕獲「XmlPullParserException」,在異常中記錄「requestDump」和 「responseDump」。另外,您應該用adb捕獲IP包。

try { 
    Logg.i(TAG, "2 "); 
    Object response = androidHttpTransport.call(SOAP_ACTION, envelope, headers); 
    Logg.i(TAG, "requestDump: " + androidHttpTransport.requestDump); 
    Logg.i(TAG, "responseDump: "+ androidHttpTransport.responseDump); 
    Logg.i(TAG, "3"); 
} catch (IOException e) { 
    Logg.i(TAG, "IOException"); 
} 
catch (XmlPullParserException e) { 
    Logg.i(TAG, "requestDump: " + androidHttpTransport.requestDump); 
    Logg.i(TAG, "responseDump: "+ androidHttpTransport.responseDump); 
    Logg.i(TAG, "XmlPullParserException"); 
    e.printStackTrace(); 
} 
-1
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // Set View to register.xml 
    setContentView(R.layout.register); 
    session = new UserSessionManeger(getApplicationContext());  

    login_id= (EditText) findViewById(R.id.loginid); 
    Suponser_id= (EditText) findViewById(R.id.sponserid); 
    name=(EditText) findViewById(R.id.name); 
    pass=(EditText) findViewById(R.id.pass); 
    moblie=(EditText) findViewById(R.id.mobile); 
    email= (EditText) findViewById(R.id.email); 
    placment= (EditText) findViewById(R.id.placement); 
    Adress= (EditText) findViewById(R.id.adress); 
    State = (EditText) findViewById(R.id.state); 

    city=(EditText) findViewById(R.id.city); 
    pincopde=(EditText) findViewById(R.id.pincode); 
    counntry= (EditText) findViewById(R.id.country); 

    plantype= (EditText) findViewById(R.id.plantype); 


    mRegister = (Button)findViewById(R.id.registration); 
    // session.createUserLoginSession(info.getCustomerID(),info.getName(),info.getMobile(),info.getEmailID(),info.getAccountType()); 
    mRegister.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 


      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);   

       request.addProperty("LoginCustomerID",login_id.getText().toString()); 
       request.addProperty("SponsorID",Suponser_id.getText().toString()); 
       request.addProperty("Name", name.getText().toString()); 
       request.addProperty("LoginPassword",pass.getText().toString()); 
       request.addProperty("MobileNumber",smoblie=moblie.getText().toString()); 
       request.addProperty("Email",email.getText().toString()); 
       request.addProperty("Placement", placment.getText().toString()); 
       request.addProperty("address1", Adress.getText().toString()); 
       request.addProperty("StateID", State.getText().toString()); 
       request.addProperty("CityName",city.getText().toString()); 

       request.addProperty("Pincode",pincopde.getText().toString()); 


       request.addProperty("CountryID",counntry.getText().toString()); 
       request.addProperty("PlanType",plantype.getText().toString()); 



       //Declare the version of the SOAP request 
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

       envelope.setOutputSoapObject(request); 
       envelope.dotNet = true; 
       try { 

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

        //this is the actual part that will call the webservice 
        androidHttpTransport.call(SOAP_ACTION1, envelope); 


        SoapObject result = (SoapObject)envelope.getResponse(); 
        Log.e("value of result", " result"+result); 
        if(result!= null) 
        { 


         Toast.makeText(getApplicationContext(), "successfully register ", 2000).show() ; 

        } 
        else { 

         Toast.makeText(getApplicationContext(), "Try Again..", 2000).show() ; 
        } 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 



     } 
    }); 
    } 

    }