2011-07-25 73 views
0

我已經看過幾個使用android和ksoap2的教程。我的服務工作正常發送一個字符串作爲從Android客戶端到肥皂服務的參數。使用kso​​ap2將對象從android傳遞到soap服務

現在,而不是傳遞一個字符串,如果我想傳遞一個對象。但是,它不適合我。

在服務器端,我有以下web服務操作(我使用jax-ws)。

package soap.service.sei; 

import javax.jws.WebService; 

import soap.service.data.ClientProfile; 

@WebService(name = "ImageSei", targetNamespace = "http://image.webservice") 
public interface ImageSei { 
    public String getImage(ClientProfile c); 
} 

很簡單。如您所見,Web服務操作採用「ClientProfile」實例。在服務器端的 'ClientProfile' 類的樣子:

package soap.service.data; 

public class ClientProfile { 
    public int ram; 
    public String cpu; 
    public String bandwidth; 
} 

再次,很簡單。但是,這應該與我的android(客戶端)包中的'ClientProfile'類完全相同嗎?

好的。在android方面,事情有點不同。我有以下用於調用Web服務的代碼片段:

ClientProfile c = new ClientProfile(); 
    c.ram = 256; 
    c.cpu = "22.445"; 
    c.bandwidth = "712.2"; 
    prop.setName("c"); 
    prop.setValue(c); 
    prop.setType(c.getClass()); 
    request.addProperty(prop); 

再次,非常簡單。在我Android項目(客戶端)我ClientProfile類樣子:

package soap.service.data; 

import java.util.Hashtable; 

import org.ksoap2.serialization.KvmSerializable; 
import org.ksoap2.serialization.PropertyInfo; 

public class ClientProfile implements KvmSerializable { 
    public int ram; 
    public String cpu; 
    public String bandwidth; 

    public ClientProfile() {} 

    public ClientProfile(int $ram, String $cpu, String $bandwidth) { 
     ram = $ram; 
     cpu = $cpu; 
     bandwidth = $bandwidth; 
    } 

    @Override 
    public Object getProperty(int arg0) { 
     switch(arg0) { 
      case 0 : 
       return ram; 
      case 1 : 
       return cpu; 
      case 2 : 
       return bandwidth; 
     } 
     return null; 
    } 

    @Override 
    public int getPropertyCount() { 
     return 3; 
    } 

    @Override 
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { 
     switch(index) { 
      case 0 : 
       info.type = PropertyInfo.INTEGER_CLASS; 
       info.name = "ram"; 
       break; 
      case 1 : 
       info.type = PropertyInfo.STRING_CLASS; 
       info.name = "cpu"; 
       break; 
      case 2 : 
       info.type = PropertyInfo.STRING_CLASS; 
       info.name = "bandwidth"; 
       break; 
      default : 
       break; 
     } 
    } 

    @Override 
    public void setProperty(int index, Object value) { 
     switch(index) { 
      case 0 : 
       ram = Integer.parseInt(value.toString()); 
       break; 
      case 1 : 
       cpu = value.toString(); 
       break; 
      case 2 : 
       bandwidth = value.toString(); 
       break; 
      default : 
       break; 
     } 
    } 
} 

然而,在服務器端,當Web服務操作是通過Android模擬器使用kso​​ap2調用,我得到「空'當我嘗試輸出作爲參數傳遞的ClientProfile實例時。這是一個真正的痛苦。

所有的教程只是向您展示來自android端的類參數,而不是web服務端。

我假設有某種序列化問題可能?無論如何,我沒有任何例外。來自Web服務的響應回到了Android客戶端,所以這不是問題。

底層問題是Web服務端的參數爲空。這裏是我的web服務操作的代碼:

@Override 
public String getImage(ClientProfile c) { 
    System.out.println(c); // ***** THIS IS NULL ***** 
    byte[] imageBytes = null; 
    try { 
      //... 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } 
    return (imageBytes != null) ? Base64.encodeBase64String(imageBytes) : ""; 
} 

我真的很感激一些幫助這裏我完全被卡住。我不知道如何打印出我的android模擬器和本地主機上運行的web服務之間的湯信息。

我問過一個關於這個問題,但不幸的是沒有得到任何答覆。

謝謝。

回答

1

我找到了答案here。對於使用JAX-WS的人,你必須包括在服務端點接口

@WebService(name = "ImageSei", targetNamespace = "http://image.webservice") 
public interface ImageSei { 
    public String getImage(@WebParam(name = "c")ClientProfile c); 
} 

參數註釋一旦你做到這一點,它應該工作。輸出如下:

[email protected] 
相關問題