【问题标题】:how to post with retrofit to WCF service如何通过改造发布到 WCF 服务
【发布时间】:2016-08-31 10:47:11
【问题描述】:

我研究了很多页,但没有一个能回答我。 这是我在android上的代码

RaceAPI.java

@FormUrlEncoded
@POST("/Service1.svc/GetData1")
void GetData1(@Field("title") List<String> m, Callback<String> cb);

这就是我发布数据的方式

  RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint("http://10.0.1.12:54253/")
            .build();
    final RaceAPI  race= adapter.create(RaceAPI.class);
    race.PostData(Arrays.asList("foo", "bar"), new Callback<String>() {
        @Override
        public void success(String s, Response response) {

        }

        @Override
        public void failure(RetrofitError error) {
            int a;
            a=1;
        }
    });

这是我的 WCF 服务签名

            [OperationContract]
            [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "/PostData")]
            String PostData(List<String> m);

但我在 Android 上的改造回调失败()中收到此错误

retrofit.RetrofitError: 400 Bad Request

任何帮助将不胜感激!

【问题讨论】:

    标签: android api wcf retrofit


    【解决方案1】:

    确实,我在改造界面上做了一些改动。

    在接口中的方法声明中添加了 Headers 属性。

    @Headers("Content-Type:application/json")
    @POST("/Service1.svc/PostData")
    void PostData(@Body SpeechModel m, Callback<SpeechModel> cb);
    

    我将要传递的数据与接口主体绑定。(我删除了@FormUrlEncoded)

    我制作了自定义类来从服务器发送和检索。我无法以字符串或字符串数​​组的形式发送(可能有某种方式。找不到它。)

       SpeechModel m = new SpeechModel();
            m.setSpeechId(5);
            m.setSpeechText("something");
            race.PostData(m, new Callback<SpeechModel>() {
                @Override
                public void success(SpeechModel PostDataResult, Response response) {
    
                }
    
                @Override
                public void failure(RetrofitError error) {
                    int a;
                    a=1;
                }
            });
    

    WCF 服务签名,更改了参数类型和返回类型。

      [OperationContract]
      [WebInvoke(Method = "POST",            
      RequestFormat = WebMessageFormat.Json,
      ResponseFormat = WebMessageFormat.Json,
      BodyStyle= WebMessageBodyStyle.Wrapped,
      UriTemplate = "/PostData")]
      SpeechModel PostData(SpeechModel m);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 2011-02-26
      • 1970-01-01
      相关资源
      最近更新 更多