我正在使用ksoap2在我的android應用中調用Web服務。它在很多場合都有效,但不知何故我無法通過這個。我打電話使用另一個類的活動我下面提到的web服務:如何使用ksoap2獲得Web服務響應
public class ServiceCall {
public static String loginID;
public static String password;
public static String authStatus;
public static String firstName;
public static String lastName;
private static final String SOAP_ACTION = "http://www.tempuri.us/";
private static final String NAMESPACE = "http://www.tempuri.us/";
private static final String URL = "http://test.tempuriprojects.com/WebServices/GetWorkData.asmx";
private boolean isResultVector = true;
protected Object call(String soapAction, SoapSerializationEnvelope envelope){
Object result = null;
final HttpTransportSE transportSE = new HttpTransportSE(URL);
transportSE.debug = false;
//call and Parse Result.
try{
transportSE.call(soapAction, envelope);
if (!isResultVector){
result = envelope.getResponse();
Log.e("RESULT: ", result.toString());
} else {
result = envelope.bodyIn;
Log.e("RESULT1: ", envelope.bodyIn.toString());
}
} catch (final IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
} catch (final Exception e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public ArrayList<DashboardEntry> getDashboardEntries(DashboardEntryCriteria bean){
ArrayList<DashboardEntry> dashboardEntryList = new ArrayList<DashboardEntry>();
try{
String methodName = "GetDashboardEntries";
// Create the outgoing message
final SoapObject requestObject = new SoapObject(NAMESPACE, methodName);
bean.setLoginID("100");
bean.setPassword("12345");
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setName("dashboardentries");
propertyInfo.setType(bean.getClass());
propertyInfo.setValue(bean);
requestObject.addProperty(propertyInfo);
//create soap envelop : soap version 1.1
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.xsd = "http://tempuri.org/GetDashboardEntries.xsd";
envelope.addMapping(null, "dashboardentries", new DashboardEntryCriteria().getClass());
envelope.implicitTypes = true;
// add the outgoing object as the request
envelope.setOutputSoapObject(requestObject);
// call and Parse Result.
final Object response = this.call(SOAP_ACTION + methodName, envelope);
SoapObject soapObj = (SoapObject)response;
int j = 0;
for(int i=0; i < length; i++)
{
SoapObject result = (SoapObject) soapObj.getProperty(i);
if(soapObj != null){
if(result.hasProperty("AuthStatus") && String.valueOf(result.getProperty("AuthStatus").toString()).equals("Y")){
loginID = result.getProperty("LoginID").toString();
System.out.println(loginID);
password = result.getProperty("Password").toString();
System.out.println(password);
authStatus = result.getProperty("AuthStatus").toString();
System.out.println(authStatus);
// Irrelevant code
}
}
}
}
catch(Exception e){
Log.e("Exception: ", e.toString());
}
return dashboardEntryList ;
}
這是DashboardEntryCriteria豆:
public class DashboardEntryCriteria implements KvmSerializable{
private String LoginID;
private String Password;
/**
* @return the password
*/
public String getPassword() {
return Password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
Password = password;
}
/**
* @return the loginID
*/
public String getLoginID() {
return LoginID;
}
/**
* @param loginID the loginID to set
*/
public void setLoginID(String loginID) {
LoginID = loginID;
}
}
@Override
public Object getProperty(int arg0) {
switch(arg0){
case 0 : return LoginID;
case 1 : return Password;
}
return null;
}
@Override
public int getPropertyCount() {
// TODO Auto-generated method stub
return 2;
}
@SuppressWarnings("rawtypes")
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
switch(arg0){
case 0 :
arg2.name = "LoginID";
arg2.type = PropertyInfo.STRING_CLASS;
arg2.namespace = "http://tempuri.org/RxGetWorkEntries.xsd";
break;
case 1 :
arg2.name = "Password";
arg2.type = PropertyInfo.STRING_CLASS;
arg2.namespace = "http://tempuri.org/RxGetWorkEntries.xsd";
break;
}
}
@Override
public void setProperty(int arg0, Object arg1) {
switch(arg0){
case 0 :
LoginID = arg1.toString();
break;
case 1 :
Password = arg1.toString();
break;
}
}
} 我肯定錯誤是在類ServiceCall某處方法getDashboardEntries()
。我注意到當調用方法call
時,它給出了空的響應(或「結果」對象)。我曾嘗試使用相同的參數使用soapUI調用Web服務,它工作得很好。只是爲了讓您知道Web服務沒有任何問題。因此,任何想法還有什麼可能是錯誤的或失蹤?
謝謝!
而你的網址應該是這樣的。 NAMESPACE +/PROJECTNAME/SERVICE_NAME.asmx – Waqas
如果您的webservice位於本地主機上,那麼您必須提供您的IP。那麼你的URL將是URL =「http://192.168.2.140/WebServices/GetWorkData.asmx」; – Waqas
我的NAMESPACE和soapAction完全一樣。我已經初始化肥皂行動作爲我的名字空間,但我通過「soapAction + methodName」,因爲我的代碼中有多個方法。另外我的web服務不在本地主機上。正如我在問題中提到的,雖然有幾個教程和一些方法工作得很好。這段代碼段必須有一些特別的錯誤和/或缺失。如果你能爲我指出這一點,我會非常感激。謝謝。 – Harsh