首先新建一个ASP.NET Web服务应用程序,我这里将其命名为ShopService,具体代码如下:
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq; using BLL.Shop; using System.Web.Script.Services; namespace WebService { /// <summary> /// ShopService 的摘要说明 /// </summary> [WebService(Namespace = "http://具体的根据你的需要咯/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 [System.Web.Script.Services.ScriptService] public class ShopService : System.Web.Services.WebService { [WebMethod] //[ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string getShopItems(string userId, int type) { Shop shop = new Shop(); return shop.getShopItems(userId, type); } } }
然后再来看看android客户端,我就直接上代码了
package com.xxx; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.json.JSONObject; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class WebserviceTookit { // private static final String TAG = "ShopItems"; private static final String WEBSERVICE_IP = "http://10.0.2.2:14294"; public static String getShopItems(String userId, int type) { try { // 调用带参数的WebMethod final String SERVER_URL = WEBSERVICE_IP + "/ShopService.asmx/getShopItems"; // 带参数的WebMethod HttpPost request = new HttpPost(SERVER_URL); // 根据内容来源地址创建一个Http请求 request.addHeader("Content-Type", "application/json; charset=utf-8");// 必须要添加该Http头才能调用WebMethod时返回JSON数据 JSONObject jsonParams = new JSONObject(); jsonParams.put("userId", userId);// 传参 jsonParams.put("type", type); HttpEntity bodyEntity = new StringEntity(jsonParams.toString(), "utf8");// 参数必须也得是JSON数据格式的字符串才能传递到服务器端,否则会出现"{'Message':'xxx是无效的JSON基元'}"的错误 request.setEntity(bodyEntity); HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is // established. // The default value is zero, that means the timeout is not used. int timeoutConnection = 3 * 1000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = 5 * 1000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); HttpResponse httpResponse = httpClient.execute(request); // 发送请求并获取反馈 // 解析返回的内容 if (httpResponse.getStatusLine().getStatusCode() == 200) // StatusCode为200表示与服务端连接成功 { StringBuilder builder = new StringBuilder(); BufferedReader bufferedReader2 = new BufferedReader( new InputStreamReader(httpResponse.getEntity() .getContent())); for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2.readLine()) { builder.append(s); } String resultString = builder.toString(); resultString = JSONToolkit.removeEscape(resultString); return resultString; } } catch (Exception e) { e.printStackTrace(); return ""; } return ""; } }
运行时记得关闭防火墙或者添加例外端口~