Android 調用 WCF

來源:互聯網
上載者:User

近期工作中項目需要開發一個Android用戶端,調用WCF服務擷取資料。我之前沒有做過Android上的Web Service開發,臨時查了一下,整理出一個完整的例子,備忘。

測試代碼下載

 

1、建立一個WCF服務項目:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2、添加一個很簡單的方法,輸出一個字串:


 

 

 

 

 

 

 

 

 

 

注意UriTemplate 的寫法,多個參數依此類推。BodyStyle 指定為允許多個參數,資料格式使用移動終端友好的Json格式,XML資料冗餘性太大。

 

 

 

 

 

 

 

 

 

3、修改 web.config

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

使用webHttpBinding 針對於 Restful Web Service,若使用basicHttpBinding則針對於 SOAP Web Service。

 

4、編譯成功後,使用VS 發布到IIS中:

先在IIS中建立一個網站:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

然後發布到該網站中:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發布成功後,在瀏覽器中查看WCF服務確認發布成功:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5、下面開始開發Android用戶端測試程式,在Eclipse中建立一個Android項目:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6、就放置一個按鈕,點擊按鈕時調用WCF服務:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7、若調用成功,記錄一條日誌:

package com.brooks.wcfdemo;

 

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

 

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import
org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.protocol.HTTP;

 

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.View;

 

public
class MainActivity extends Activity {

 


@Override


public
void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

 


@Override


public
boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.activity_main, menu);


return
true;

}

 


public
void WCFTestLinstener(View view)

{

    new Thread(WCFTest).start();

}

 

Runnable WCFTest = new Runnable(){

        public
void run() {

         // TODO Auto-generated method stub

            CallWCF();

            }

};

 


private
void CallWCF() {


try {


// Send GET request to <service>/GetPlates


HttpGet request = new
HttpGet("http://192.168.0.100:90/AndroidService.svc/fnTest/WCF");

request.setHeader("Accept", "application/json");

request.setHeader("Content-type", "application/json");

 

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpResponse response = httpClient.execute(request);

 

HttpEntity responseEntity = response.getEntity();

 

Log.d("WCF", retrieveInputStream(responseEntity));

} catch (Exception e) {

e.printStackTrace();

}

}

 


protected String retrieveInputStream(HttpEntity httpEntity) {


int length = (int) httpEntity.getContentLength();


if (length < 0)

length = 10000;

StringBuffer stringBuffer = new StringBuffer(length);


try {

InputStreamReader inputStreamReader = new InputStreamReader(

httpEntity.getContent(), HTTP.UTF_8);


char buffer[] = new
char[length];


int count;


while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {

stringBuffer.append(buffer, 0, count);

}

} catch (UnsupportedEncodingException e) {

 

} catch (IllegalStateException e) {

 

} catch (IOException e) {

 

}


return stringBuffer.toString();

}

}

 

 

因為Android 高版本對主線程上的操作做了限制,故需要非同步呼叫。

首先使用模擬器測試:

手機開啟Wifi後,就可以在真機進行測試了:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.