2013-06-18 101 views
0

嗨我在我的應用程序中使用soap服務。 soap服務運行mozilla firefox REST客戶端和SOA客戶端。ksoap2 android無法訪問soap服務?

請求正文:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"> 
<s:Header> 
<a:Action s:mustUnderstand="1">http://tempuri.org/IService/GetData</a:Action> 
</s:Header> 
<s:Body> 
<GetData xmlns="http://tempuri.org/"><value>Hello</value></GetData> 
</s:Body> 
</s:Envelope> 

代碼:

private final String raw1 = "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://www.w3.org/2005/08/addressing\">" + 
      "<s:Header>" + 
      "<a:Action s:mustUnderstand=\"1\">http://tempuri.org/IService/GetData</a:Action>" + 
      "</s:Header>" + 
      "<s:Body>" + 
      "<GetData xmlns=\"http://tempuri.org/\">" + 
      "<value>Hello</value>" + 
      "</GetData>" + 
      "</s:Body>" + 
      "</s:Envelope>"; 
HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost(URL); 
post.setHeader("Content-type", "application/soap+xml"); 
post.setHeader("charset", "utf-8"); 
try { 
    post.setEntity(new StringEntity(raw1, "UTF8")); 
} catch (UnsupportedEncodingException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
try { 
    HttpResponse response = client.execute(post); 
    Log.d("test", ""+response.getStatusLine()); 
    InputStream is = response.getEntity().getContent(); 
    BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
    String str, res = ""; 
    while((str = br.readLine()) != null) { 
     res += str; 
    } 
    br.close(); 
    is.close(); 
    Log.d("test", ""+res); 
} catch(Exception e) { 
    e.printStackTrace(); 
} 

如果我用這個原始請求主體,並進行HTTP POST手動我能夠打服務在我的Android應用程序,但如果我使用kso​​ap2

private String NAME_SPACE = "http://tempuri.org"; 
private String METHOD_NAME = "GetData"; 
private final String URL = "http://example.amazonaws.com/PresenceServices/Service.svc"; 
private final String SOAP_ACTION = "http://tempuri.org/IService/GetData"; 

SoapObject request = new SoapObject(NAME_SPACE, METHOD_NAME); 
request.addProperty("value", "hello"); 
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); 
envelope.dotNet = true; 
envelope.setOutputSoapObject(request); 
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
try { 
    androidHttpTransport.call(SOAP_ACTION, envelope); 
    SoapObject response = (SoapObject)envelope.getResponse(); 
    Log.e("Object response", response.toString()); 
    return response; 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

它拋出IOException Http請求失敗Http狀態500

請幫助我在ksoap2中實現此功能。

+1

如果可能的話,我們展示的WSDL文件或給WSDL –

+0

@PareshMayani的鏈接:對不起,我不能提供wsdl文件 –

回答

0

我認爲問題在於您沒有將信頭添加到信封中。 (Action頭)

嘗試

private Element[] buildHeader(String URL, String SOAP_ACTION) { 
    List<Element> headers = new ArrayList<Element>(); 
    Element action = new Element().createElement(
      "http://www.w3.org/2005/08/addressing", "Action"); 
    action.addChild(Node.TEXT, SOAP_ACTION); 
    action.setAttribute("http://www.w3.org/2005/08/addressing", 
      "mustUnderstand", "1"); 
    headers.add(action); 

    int size = headers.size(); 
    Element[] array = new Element[size]; 
    for (int i = 0; i < size; i++) 
     array[i] = headers.get(i); 
    return array; 
} 

而且

Element[] e = buildHeader(url, SOAP_ACTION); 
soapEnvelope.headerOut = e;