初学android,这个问题困扰了多天,在搜索引擎及论坛各位高手的帮助下,现在终于给搞定了,在这里分享给大家,希望对大家有所帮助!(其实本来是尝试用SOAP协议的,试了n天,无果,无奈又只能回到http了!)先来看看服务器端吧!

首先新建一个ASP.NET Web服务应用程序,我这里将其命名为ShopService,具体代码如下:
C#代码
  1. usingSystem;
  2. usingSystem.Collections;
  3. usingSystem.ComponentModel;
  4. usingSystem.Data;
  5. usingSystem.Linq;
  6. usingSystem.Web;
  7. usingSystem.Web.Services;
  8. usingSystem.Web.Services.Protocols;
  9. usingSystem.Xml.Linq;
  10. usingBLL.Shop;
  11. usingSystem.Web.Script.Services;
  12. namespaceWebService
  13. {
  14. ///<summary>
  15. ///ShopService的摘要说明
  16. ///</summary>
  17. [WebService(Namespace="http://具体的根据你的需要咯/")]
  18. [WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
  19. [ToolboxItem(false)]
  20. //若要允许使用ASP.NETAJAX从脚本中调用此Web服务,请取消对下行的注释。
  21. [System.Web.Script.Services.ScriptService]
  22. publicclassShopService:System.Web.Services.WebService
  23. {
  24. [WebMethod]
  25. //[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
  26. publicstringgetShopItems(stringuserId,inttype)
  27. {
  28. Shopshop=newShop();
  29. returnshop.getShopItems(userId,type);
  30. }
  31. }
  32. }

然后再来看看android客户端,我就直接上代码了
Java代码
  1. packagecom.xxx;
  2. importjava.io.BufferedReader;
  3. importjava.io.InputStream;
  4. importjava.io.InputStreamReader;
  5. importjava.net.HttpURLConnection;
  6. importjava.net.URL;
  7. importorg.apache.http.HttpEntity;
  8. importorg.apache.http.HttpResponse;
  9. importorg.apache.http.client.methods.HttpPost;
  10. importorg.apache.http.entity.StringEntity;
  11. importorg.apache.http.impl.client.DefaultHttpClient;
  12. importorg.apache.http.params.BasicHttpParams;
  13. importorg.apache.http.params.HttpConnectionParams;
  14. importorg.apache.http.params.HttpParams;
  15. importorg.json.JSONObject;
  16. importandroid.graphics.Bitmap;
  17. importandroid.graphics.BitmapFactory;
  18. publicclassWebserviceTookit{
  19. //privatestaticfinalStringTAG="ShopItems";
  20. privatestaticfinalStringWEBSERVICE_IP="http://10.0.2.2:14294";
  21. publicstaticStringgetShopItems(StringuserId,inttype){
  22. try{
  23. //调用带参数的WebMethod
  24. finalStringSERVER_URL=WEBSERVICE_IP
  25. +"/ShopService.asmx/getShopItems";//带参数的WebMethod
  26. HttpPostrequest=newHttpPost(SERVER_URL);//根据内容来源地址创建一个Http请求
  27. request.addHeader("Content-Type","application/json;charset=utf-8");//必须要添加该Http头才能调用WebMethod时返回JSON数据
  28. JSONObjectjsonParams=newJSONObject();
  29. jsonParams.put("userId",userId);//传参
  30. jsonParams.put("type",type);
  31. HttpEntitybodyEntity=newStringEntity(jsonParams.toString(),
  32. "utf8");//参数必须也得是JSON数据格式的字符串才能传递到服务器端,否则会出现"{'Message':'xxx是无效的JSON基元'}"的错误
  33. request.setEntity(bodyEntity);
  34. HttpParamshttpParameters=newBasicHttpParams();
  35. //Setthetimeoutinmillisecondsuntilaconnectionis
  36. //established.
  37. //Thedefaultvalueiszero,thatmeansthetimeoutisnotused.
  38. inttimeoutConnection=3*1000;
  39. HttpConnectionParams.setConnectionTimeout(httpParameters,
  40. timeoutConnection);
  41. //Setthedefaultsockettimeout(SO_TIMEOUT)
  42. //inmillisecondswhichisthetimeoutforwaitingfordata.
  43. inttimeoutSocket=5*1000;
  44. HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
  45. DefaultHttpClienthttpClient=newDefaultHttpClient(httpParameters);
  46. HttpResponsehttpResponse=httpClient.execute(request);//发送请求并获取反馈
  47. //解析返回的内容
  48. if(httpResponse.getStatusLine().getStatusCode()==200)//StatusCode为200表示与服务端连接成功
  49. {
  50. StringBuilderbuilder=newStringBuilder();
  51. BufferedReaderbufferedReader2=newBufferedReader(
  52. newInputStreamReader(httpResponse.getEntity()
  53. .getContent()));
  54. for(Strings=bufferedReader2.readLine();s!=null;s=bufferedReader2.readLine()){
  55. builder.append(s);
  56. }
  57. StringresultString=builder.toString();
  58. resultString=JSONToolkit.removeEscape(resultString);
  59. returnresultString;
  60. }
  61. }catch(Exceptione){
  62. e.printStackTrace();
  63. return"";
  64. }
  65. return"";
  66. }
  67. }


运行时记得关闭防火墙或者添加例外端口~

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. Android(安卓)NDK 中使用C++源文件和使用C文件的不同
  3. 如何将uboot里面的参数传给recovery或kernel
  4. 重磅来袭!2020 年需要关注的 5 大 Android(安卓)开发技术,抓破脑袋
  5. Android的Linux“心”
  6. Android(安卓)自动点击
  7. Android(安卓)Studio JNI学习之(3)-使用SWIG自动生成Jni代码
  8. Android学习之路——简易版微信为例(二)
  9. android中的数据存取-方式一:preference(配置)

随机推荐

  1. Android《第三章:AIDL与Messenger》
  2. 关于Android横竖屏切换的解决方法
  3. Textview的maxLines大于1行时,"android:el
  4. Android(安卓)NDK: Host 'awk' tool is o
  5. 完美Android(安卓)Cursor使用例子(Android
  6. Android之A面试题③应用程序启动过程源代
  7. Android(安卓)Service之MountService源码
  8. selector背景以及android的透明色
  9. Android系统Surface机制的SurfaceFlinger
  10. 理解android中ListFragment和Loader