2016-05-11 14 views
-1

我需要从我的android应用程序调用肥皂webservice.webservice方法有一个对象有4个值。我已阅读有关ksoap lib.But我很困惑如何服务对象作为参数从android.Please帮助我解决这个问题。肥皂webservice方法与Android的对象被调用

public SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL,String IP,String SERVICEPATH) throws IOException, XmlPullParserException 
    { 

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request 

    PropertyInfo pi = new PropertyInfo(); 

    pi.setName("Parameter1"); 
    pi.setValue(Value1); 

    request.addProperty(pi); 
    pi.setName("Parameter2"); 
    pi.setValue(Value2); 
    request.addProperty(pi); 



SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // put all required data into a soap 
envelope.dotNet = true; 
envelope.setOutputSoapObject(request); // prepare request 
envelope.bodyOut = request; 
    Log.d("ENVELOPE",""+"Coming3"); 
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
//androidHttpTransport. 
androidHttpTransport.call(SOAP_ACTION, envelope); 

Log.d("ENVELOPE",""+envelope.bodyIn); 
SoapObject result = (SoapObject) envelope.bodyIn; // get response 
Log.d("ENVELOPE",""+envelope.bodyIn); 
SoapObject responseBodyRaw,responseBody,tableRow; 
return result; 
} 

这是应该怎么做? 我没有使用kso​​ap和肥皂webservices.Please帮助

+0

你的代码在哪里?邮编至少 –

+0

是的Ksoap lib是好的。但是,你得到什么错误/什么问题?你见过这个http://programmerguru.com/android-tutorial/how-to-call-java-web-service-in -android/ –

+0

什么是你必须发送请求的格式(结构)....? –

回答

0

假设你有webmethod这样定义返回Category对象。

[WebMethod] 
public Category GetCategoryById(Category C); 

说Category类有三个属性,如类别编号,名称和Description.So与KSOAP使用它,你必须实现KvmSerializable

package CommonObjects; 

import java.util.Hashtable; 
import org.ksoap2.serialization.KvmSerializable; 
import org.ksoap2.serialization.PropertyInfo; 

public class Category implements KvmSerializable 
{ 
public int CategoryId; 
public String Name; 
public String Description; 

public Category(){} 
public Category(int categoryId, String name, String description) { 

    CategoryId = categoryId; 
    Name = name; 
    Description = description; 
} 
public Object getProperty(int arg0) { 

    switch(arg0) 
    { 
    case 0: 
     return CategoryId; 
    case 1: 
     return Name; 
    case 2: 
     return Description; 
    } 

    return null; 
} 

public int getPropertyCount() { 
    return 3; 
} 
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { 
    switch(index) 
    { 
    case 0: 
     info.type = PropertyInfo.INTEGER_CLASS; 
     info.name = "CategoryId"; 
     break; 
    case 1: 
     info.type = PropertyInfo.STRING_CLASS; 
     info.name = "Name"; 
     break; 
    case 2: 
     info.type = PropertyInfo.STRING_CLASS; 
     info.name = "Description"; 
     break; 
    default:break; 
    } 
} 

public void setProperty(int index, Object value) { 
    switch(index) 
    { 
    case 0: 
     CategoryId = Integer.parseInt(value.toString()); 
     break; 
    case 1: 
     Name = value.toString(); 
     break; 
    case 2: 
     Description = value.toString(); 
     break; 
    default: 
     break; 
    } 
} 
} 

现在你必须映射属性,读取它们并写入它们。

,使Web服务调用是:

public void WebServiceCallExample() 
    { 
     String NAMESPACE = "http://vladozver.org/"; 
     String METHOD_NAME = "GetCategoryById"; 
     String SOAP_ACTION = "http://vladozver.org/GetCategoryById"; 
     String URL = "http://192.168.1.3/VipEvents/Services/CategoryServices.asmx"; 

     SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 

     /* 
     * Create Category with Id to be passed as an argument 
     * 
     * */ 
     Category C = new Category(); 
     C.CategoryId = 1; 

     /* 
     * Set the category to be the argument of the web service method 
     * 
     * */ 
     PropertyInfo pi = new PropertyInfo(); 
     pi.setName("C"); 
     pi.setValue(C); 
     pi.setType(C.getClass()); 
     Request.addProperty(pi); 

     /* 
     * Set the web service envelope 
     * 
     * */ 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(Request); 

     envelope.addMapping(NAMESPACE, "Category",new Category().getClass()); 
     AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL); 
     /* 
     * Call the web service and retrieve result ... how luvly <3 
     * 
     * */ 
     try 
     { 
      androidHttpTransport.call(SOAP_ACTION, envelope); 
      SoapObject response = (SoapObject)envelope.getResponse(); 
      C.CategoryId = Integer.parseInt(response.getProperty(0).toString()); 
      C.Name = response.getProperty(1).toString(); 
      C.Description = (String) response.getProperty(2).toString(); 
      TextView tv = (TextView)findViewById(R.id.TextView01); 
      tv.setText("CategoryId: " +C.CategoryId + " Name: " + C.Name + " Description " + C.Description); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

为了使用此代码,请确保您替换该方法与自己的前三个变量。注意:SOAP_ACTION实际上可以计算为NAMESPACE + METHOD_NAME

在关于属性信息的一行中,我演示了如何将一个Category类复杂对象实际传递给KSOAP。

PropertyInfo pi = new PropertyInfo(); 
pi.setName("Category"); 
pi.setValue(C); 
pi.setType(C.getClass()); 
Request.addProperty(pi); 

关于返回类型,如果你的Web方法返回一个复杂的对象(如我们的),你需要告诉KSOAP如何处理响应。即用下面的代码完成:

envelope.addMapping(NAMESPACE, "Category",new Category().getClass()); 

的这部分代码是响应的实际检索:从这个blogpost

androidHttpTransport.call(SOAP_ACTION, envelope); 
SoapObject response = (SoapObject)envelope.getResponse(); 
C.CategoryId = Integer.parseInt(response.getProperty(0).toString()); 
C.Name = response.getProperty(1).toString(); 
C.Description = (String) response.getProperty(2).toString(); 

参考。

+0

嗨...感谢代码..我还有1个疑问..我有一个字节[]和日期在类别类...所以这些值的类型应该是什么 – angh

+0

我需要添加7个值和它会返回一个字符串。所以在类别类中我应该设置所有的值? – angh

+0

嗨...我得到xmlPullParseException:意外的令牌@ 3:15 ...请帮助我..解决它 – angh