如何调用一个asp.net web服务? [英] How to Invoke an asp.net web service?

查看:96
本文介绍了如何调用一个asp.net web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个有两个方法,首先是 getBoughtApps(IMEI串)一个asp.net web服务
另一种是 getAllApps()。该服务名为 ArttechApps 。我需要一个code调用这些方法使用Java或Android
谁能帮助我?

I created an asp.net web service that has two methods the first is getBoughtApps(String imei) and the other one is getAllApps() . The service name is ArttechApps . I need a code that invokes these methods using java or android can anyone help me ?

推荐答案

您应该编写一个返回XML或JSON到客户端的middlieware服务。

You should write an middlieware service that returns xml or json to your client.

与reciever.aspx创建一个新的Web项目,在您的IIS

Create a new web project, on your iis with reciever.aspx

清除aspx文件codeS,只剩

clear your aspx file codes, only left ,

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="reciever.aspx.cs" Inherits="reciever" %>

那么你的code页(.cs文件)

then your code page (.cs file)

添加您的服务项目

在页面加载

 if (Request.QueryString["ID"] != null)
    {
        string id = Request.QueryString["id"]; // ID paremeter will be taken from your client.I will explain.

         if (id == "getBoughtApps")
        {
           string imei= Request.QueryString["imei"];  
           // IMEI paremeter will be taken from your client.I will explain.

            // Use your service method here then create an xml and write 
            // look at Example below 

            DataSet ds = SqlHelper.ExecuteDataset(connStr, CommandType.StoredProcedure, sp_Get_BoughtApps"
                , new SqlParameter("@imei", imei));

            string str = @"<?xml version=""1.0"" encoding=""utf-8"" ?><test>";

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                str += "<item>";
                str += "<ID>" + dr["ID"].ToString() + "</ID>";
                str += "<appName>" + dr["appName"].ToString() + "</appName>";
                str += "</item>";
            }

            str += @"</test>";

            Response.Write(str); // it returns an xml and your client will catch it

        }

        if (id == "getAllApps")
        {
              // write your code
        }

    }

在你的客户端,

private class asynGetBoughtApps extends AsyncTask<String, Void, Integer> {
    protected Integer doInBackground(String... params) {
        try {
            final Uri.Builder uri = new Uri.Builder();
            uri.scheme("http");
            uri.authority(ip or domainname); // "127.0.0.1"
            uri.path(alias name ); // "android/reciever.aspx"
            uri.appendQueryParameter("id", "getBoughtApps"); // you can sent quaerystring with this
            uri.appendQueryParameter("imei", "35464545454");


            URL url = new URL(uri.toString());
            DocumentBuilderFactory dbf = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("item"); // xml node which will loop in

            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);
                Element fstElmnt = (Element) node;

                NodeList nodelist = null;
                Element element = null;

                nodelist = fstElmnt.getElementsByTagName("ID"); 
                element = (Element) nodelist.item(0);
                nodelist = element.getChildNodes();
                if ((nodelist.item(0)) != null)
                    string ID = (nodelist.item(0)).getNodeValue().toString();

                                    nodelist = fstElmnt.getElementsByTagName("appName");
                element = (Element) nodelist.item(0);
                nodelist = element.getChildNodes();
                if ((nodelist.item(0)) != null)
                    string appName= (nodelist.item(0)).getNodeValue().toString();


            }
        } catch (Exception e) {             
            return 0;
        }

        return 1;
    }

    protected void onPostExecute(Integer result) {
        try {
            // Now you get data from your server and you can use it in your app
        } catch (Exception e) {             
        }

        super.onPostExecute(result);
    }
}

这篇关于如何调用一个asp.net web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆