【问题标题】:wcf rest and android post methodwcf rest和android post方法
【发布时间】:2014-01-20 12:33:17
【问题描述】:

我已经使用 vs 模板写了一个 wcf rest 我在 c# 中的帖子看起来像这样

[LWebInvoke(UriTemplate ="",Method="POST")]
public DMSDateTime Create( DMSDateTime instance)
{
return instance;

 }

DMSDateTime 低于

public class DMSDateTime()
{
 public DMSDateTime(){
 Date =System. DateTime.Now.ToString("yyyy-MM-dd");
 Time=System. DateTime.Now.ToString("hh:mm:ss");
 }

 public string Date{get; set;}
 public string Time{get; set;}

 }
 }

Android 部分如下

 String s ="<DMSDateTime>\r\n<Date>20-01-2014</Date>\r\n<Time>06:00:00</Time>\r\n</DMSDateTime>";
 HttpClient httpclient =new DefaultHttpClient();
 HttpPost request = new     HttpPost("http://test.com:4567/disservice/");
 StringEntity sa = new StringEntity(s);
 sa.setContentEncoding("UTF-8");
 sa.setContentType("text/XML");
 request.setEntity(sa);
 httpclient.execute(request);

我收到了错误的请求,因为响应状态代码 400 请帮助...请原谅我的缩进不正确我的公司不允许发布代码所以我看到了代码并在我的手机上输入...

如果我提出 get 请求,它工作正常

【问题讨论】:

    标签: android rest post methods service


    【解决方案1】:

    像这样编写您的网络服务:-

    [OperationContract]
            [WebInvoke(Method = "POST",
              ResponseFormat = WebMessageFormat.Json,
              BodyStyle = WebMessageBodyStyle.Wrapped,
              UriTemplate = "Login?parameter={parameter}")]
              string Login(string parameter);
    
    
         public string Login(string parameter)
            {
    
    
    
                /*
                 * input :=  {"username":"kevin","password":"123demo"}
                 * output:=  1=sucess,0=fail
                 *
                */
    
                //Getting Parameters from Json  
                JObject jo = JObject.Parse(parameter);
                string username = (string)jo["username"];
                string password = (string)jo["password"];
                return ""+username;
        }
    

    并像这样在 Android 中访问此服务:-

    public static String getJsonData(String webServiceName,String parameter)
    {  
        try  
        {
        String urlFinal=SERVICE_URI+"/"+webServiceName+"?parameter=";
        HttpPost postMethod = new HttpPost(urlFinal.trim()+""+URLEncoder.encode(parameter,"UTF-8"));
        postMethod.setHeader("Accept", "application/json");
        postMethod.setHeader("Content-type", "application/json");
    
        HttpClient hc = new DefaultHttpClient();
    
        HttpResponse response = hc.execute(postMethod);
        Log.i("response", ""+response.toString());
        HttpEntity entity = response.getEntity();
        final String responseText = EntityUtils.toString(entity);
    
        string=responseText;
        Log.i("Output", ""+responseText);
          }
          catch (Exception e) {
    
        }
    
    return string;
    }
    

    【讨论】:

    • 如果我只想使用 XML 没有 json 怎么办
    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 2014-03-07
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多