2012-11-07 160 views
-1

我想將以下請求格式發送給其餘的webservice我該如何做?如何在android中發送http請求?

<?xml version="1.0" encoding="UTF-8"?> 
<demo> 
<headers> 
    <messagetype>1</messagetype> 
    <messagetoken>123647356734156</messagetoken> 
</headers> 
<authentication> 
    <name>xxx</name> 
    <servicename>yyy</servicename> 
    <username>10121</username> 
    <password>welcome1234</password> 
</authentication> 
</demo> 

回答

1

一個最簡單的方法是創建XML請求值的字符串。例如,您可以將此XML字符串請求值傳遞給下面的函數以從服務器獲取響應。

僅供參考,您可以通過在HTTPPost的對象內設置實體值來傳遞請求,在下例中也是一樣。您也可以通過這種方式傳遞JSON請求值。

例如:

public HttpResponse postData(String strXML) { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("web service URL"); 

    try { 
     StringEntity strEntity = new StringEntity(strXML,HTTP.UTF_8); 
     strEntity.setContentType("text/xml"); 
     httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8"); 
     httppost.setEntity(strEntity); // here you can set request value. 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 
     return response; 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 

    return null; 
} 
+0

那我怎麼讀迴應呢? – user1805605

0

嘗試reslet http://www.restlet.org/他們也有一個版本的Android ..

+0

雖然此鏈接可以回答這個問題,最好是在這裏有答案的主要部件,並提供鏈接以供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 –

+0

okie ................ –