从android调用wcf服务 - 找不到代码运行不正常的任何原因

时间:2011-12-24 01:44:28

标签: android wcf

我写了一些需要从android平台调用的wcf接口(REST)。 第一种方法在Android的任何调用上运行良好 - 但第二种方法根本不起作用,我认为没有任何理由这样开心。

客户端(android)中的代码没有任何异常 - 我只是在HttpResponse中看到消息'bad request',我没有看到代码在wcf代码上停止(在服务器中)。

WCF代码:

    [OperationContract, WebInvoke( Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare )]
    Replay_1_DTO Method_01( Stream image );


    [OperationContract, WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml,  BodyStyle = WebMessageBodyStyle.Bare)]  
    Replay_2_DTO Method_02(int val1);

第二种方法的android代码:

      Replay_2_DTO Call_Method_02(int val1)
{
     DefaultHttpClient httpclient = new DefaultHttpClient();
     HttpPost httppost = new HttpPost(WEB_SERVICE_URL);
     Replay_2_DTO replay_2_DTO = null; 
     try
     {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);        

    nameValuePairs.add(new BasicNameValuePair("val1", val1));         

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse p = httpclient.execute(httppost);

    String str = EntityUtils.toString(p.getEntity());

                replay_2_DTO = parser( str );


      } 
      catch (Exception e) { ... }

            return replay_2_DTO;

        }

1 个答案:

答案 0 :(得分:1)

此行为的最可能原因是WCF服务无法序列化replay_2_DTO或其子代之一。

要解决此问题,请将以下内容添加到web.config的部分。重现错误后,双击该文件以使用日志查看器工具,该工具应准确显示正在发生的事情。

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="traceListener"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData="c:\log\WebTrace.svclog"  />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

即使在上述建议之前,另一个好用的工具是fiddler。有时您可以在响应中看到更详细的信息,这些信息将引导您朝着正确的方向前进。

相关问题