<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    朱杰兵blog

    jonhney'blog
    posts - 140, comments - 1, trackbacks - 0, articles - 0

    通過HttpClient請求webService

    Posted on 2017-05-24 16:10 朱杰兵 閱讀(1214) 評論(0)  編輯  收藏

    通過HttpClient請求webService 

    由于服務端是用webService開發的,android要調用webService服務獲取數據,這里采用的是通過HttpClient發送post請求,獲取webService數據。
     
    服務端使用的webService框架是axis2,請求數據之前,要封裝一個xml格式,再通過post請求,獲取服務端數據。
    請求的xml格式如下所示: 
    1 <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"xmlns:sam="http://user.service.xxx.com">
    2    <soap:Header/>
    3    <soap:Body>
    4       <sam:getUserInfo>
    5      <sam:userName>sunlightcs</sam:userName>
    6       </sam:getUserInfo>
    7    </soap:Body>
    8 </soap:Envelope>
    其中:getUserInfo是方法名,userName是參數名,當然,還可以加多個參數。
     
     
    下面的代碼是向webService發送請求,獲取數據,返回的數據是xml形式的,android只要解析xml數據,就可以獲得想要的數據了。 

    01 import java.io.IOException;
    02 import java.io.OutputStream;
    03 import java.io.OutputStreamWriter;
    04 import java.io.Writer;
    05  
    06 import org.apache.http.HttpResponse;
    07 import org.apache.http.client.HttpClient;
    08 import org.apache.http.client.methods.HttpPost;
    09 import org.apache.http.entity.ContentProducer;
    10 import org.apache.http.entity.EntityTemplate;
    11 import org.apache.http.impl.client.DefaultHttpClient;
    12 import org.apache.http.util.EntityUtils;
    13  
    14  
    15 public class ClientTest {
    16  
    17     public static void main(String[] args) {
    18         ClientTest.httpClientPost();
    19     }
    20      
    21     private static void httpClientPost() {
    22         HttpClient client = new DefaultHttpClient();
    23         HttpPost post = newHttpPost("http://localhost:8080/xxx/services/userService");
    24          
    25         try {
    26             ContentProducer cp = new ContentProducer() {
    27                 public void writeTo(OutputStream outstream) throwsIOException {
    28                     Writer writer = new OutputStreamWriter(outstream,"UTF-8");
    29                      
    30                     /**
    31                      * 獲取請求的xml格式數據
    32                      */
    33                     String requestXml = getRequestXml();
    34                     writer.write(requestXml);
    35                     writer.flush();
    36                 }
    37             };
    38  
    39             post.setEntity(new EntityTemplate(cp));
    40             HttpResponse response = client.execute(post);
    41              
    42         //打印返回的xml數據
    43             System.out.println(EntityUtils.toString(response.getEntity()));
    44         catch (IOException e) {
    45             e.printStackTrace();
    46         }
    47     }
    48      
    49      
    50     private static String getRequestXml(){
    51         StringBuilder sb = new StringBuilder("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:sam=\"http://user.service.xxx.com\">");
    52         sb.append("<soap:Header/>");
    53         sb.append("<soap:Body>");
    54         sb.append("<sam:getUserInfo>");
    55         sb.append("<sam:userName>sunlightcs</sam:userName>");
    56         sb.append("</sam:getUserInfo>");
    57         sb.append("</soap:Body>");
    58         sb.append("</soap:Envelope>");
    59          
    60         return sb.toString();
    61     }
    62  
    63 }

    返回的數據格式如下: 
    1 <?xml version='1.0' encoding='UTF-8'?>
    2 <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    3     <soapenv:Body>
    4         <ns:getUserInfoResponse xmlns:ns="http://user.service.xxx.com">
    5             <ns:return>xxx</ns:return>
    6         </ns:getUserInfoResponse>
    7     </soapenv:Body>
    8 </soapenv:Envelope>
    其中,<ns:return>內的"xxx"可以是json數據,android只需解析標簽<ns:return>里的json數據即可。 
    轉載 http://www.juziku.com/wiki/3919.htm

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲男人的天堂一区二区| 永久免费无码网站在线观看个| 亚洲精品tv久久久久久久久久| 免费专区丝袜脚调教视频| 一级视频在线免费观看| 亚洲最大天堂无码精品区| 久久91亚洲精品中文字幕| 亚洲国产精品专区在线观看| 国产一精品一AV一免费孕妇| 日本免费一区二区三区四区五六区| 一级做受视频免费是看美女| 亚洲日韩中文字幕无码一区| 亚洲成人免费在线观看| 亚洲va国产va天堂va久久| 亚洲一区二区三区乱码A| 青青草国产免费久久久下载| 99re热免费精品视频观看| 91免费国产精品| 欧洲人成在线免费| 美女被免费网站91色| jizz免费观看| 曰韩无码AV片免费播放不卡| 亚洲成aⅴ人片久青草影院按摩 | 处破女第一次亚洲18分钟| 亚洲精品天堂在线观看| 亚洲成a人片在线网站| 99人中文字幕亚洲区| 亚洲一区二区三区日本久久九| 亚洲老妈激情一区二区三区| 中文字幕亚洲激情| 亚洲精品无码成人片在线观看 | 久久激情亚洲精品无码?V| 免费在线观看你懂的| 国产成人在线免费观看| 国产色婷婷精品免费视频| 青青青国产免费一夜七次郎 | 亚洲精品福利在线观看| 久久久亚洲AV波多野结衣| 亚洲电影唐人社一区二区| 亚洲性一级理论片在线观看| 亚洲三级中文字幕|