首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据传递到.net web服务并返回结果?

将数据传递到.net web服务并返回结果?
EN

Stack Overflow用户
提问于 2012-05-06 15:01:32
回答 2查看 1.4K关注 0票数 4

我需要一个有效的例子/教程如何传递输入数据从安卓到.net网络服务和检索结果到安卓应用程序。

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

EN

回答 2

Stack Overflow用户

发布于 2012-05-06 15:35:12

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

代码语言:javascript
复制
        [OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/post-customer/{name}")]
        void postCustomer(string name);

要使用客户id获取客户数据,请执行以下操作:

代码语言:javascript
复制
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/get-customer/{id}")]
        Customer getCustomer(string id);

然后,在托管您的for服务之后,您需要使用HTTP客户端从android应用程序访问它,例如:

代码语言:javascript
复制
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();

在此之后,您应该在"result“中有一个字符串,该字符串表示您的响应(json或xml)。

希望这些信息能帮助你。

票数 1
EN

Stack Overflow用户

发布于 2012-06-03 22:42:22

这里给出了一个小样本,试着在post中使用

public void post(String appid,String custlogid,String cityareaid,String mosqname,String mosqid,String lat,String lon){

代码语言:javascript
复制
        HttpClient httpclient = new DefaultHttpClient();   
        HttpPost httppost = new HttpPost(globalconstant.URL_mosquepost); 
        UrlEncodedFormEntity form;
        // Add your data   
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);   
        nameValuePairs.add(new BasicNameValuePair("APPUDID", appid));
        nameValuePairs.add(new BasicNameValuePair("CUSTLOGID", custlogid));
        nameValuePairs.add(new BasicNameValuePair("CITYAREAID", cityareaid));
        nameValuePairs.add(new BasicNameValuePair("CITYMOSQUENAME", mosqname));
        nameValuePairs.add(new BasicNameValuePair("CITYMOSQUEID", "0"));
        nameValuePairs.add(new BasicNameValuePair("LATITUDE", lat));
        nameValuePairs.add(new BasicNameValuePair("longitude", lon));
        try {
            form = new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
             httppost.setEntity(form);
            Log.d("myapp", "works till here. 2");
            try {
                HttpResponse response = httpclient.execute(httppost);
                Log.d("myapp", "response " + response.getStatusLine().getStatusCode()+"\n"+EntityUtils.toString(response.getEntity()));

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10468808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档