gpt4 book ai didi

c# - 将数据传递给 .net Web 服务并取回结果?

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:55 25 4
gpt4 key购买 nike

我需要一个有效的示例/教程,了解如何将输入数据从 Android 传递到 .net 网络服务以及如何将结果检索到 Android 应用程序。

请分享任何您认为与我的需求相关的指南、示例和教程的链接。

最佳答案

您可以创建一个 Rest Web 服务,这里是一个 tutorial ,你可以使用 URI 传递输入数据,你应该计划你的 URI 应该如何,例如发布客户名称:

        [OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/post-customer/{name}")]
void postCustomer(string name);

使用客户 ID 获取客户数据:

        [OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/get-customer/{id}")]
Customer getCustomer(string id);

然后在托管您的网络服务之后,您需要使用 HTTP 客户端从您的 Android 应用访问它:

String uri = "uri to your service";
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(uri);
ResponseHandler<String> handler = new BasicResponseHandler();
String result = null;
try {
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();

之后你应该在“结果”中有一个字符串,这个字符串代表你的响应(json 或 xml)。

希望这些信息可以帮助到您。

关于c# - 将数据传递给 .net Web 服务并取回结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10468808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com