Android未连接到在浏览器中工作的WCF服务

时间:2014-06-21 12:13:07

标签: java c# android web-services wcf

我有 WCF服务(在浏览器中尝试过它)代码

[OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Login/?username={username}&password={password}")]
    bool Login(string username, string password);

    [OperationContract]
    [WebGet(UriTemplate = "Search/?query={query}&type={type}", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    List<AndroidBook> Search(string query, int type);

编辑:网络配置文件:

<system.serviceModel>
    <services>
      <service name="??????.AndroidBridge">
        <endpoint address="" binding ="webHttpBinding" contract="??????.IAndroidBridge" behaviorConfiguration="webHttpBehavior"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="" >
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp/>
        </behavior>
        <behavior name="webHttpBehavior">
          <webHttp automaticFormatSelectionEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add scheme="http" binding="webHttpBinding"/>
    </protocolMapping>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel> 

在浏览器中测试时,我得到以下内容:

{"LoginResult":true}

这是真的,但在尝试使用此代码连接Android客户端时:

try {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpResponse response = httpclient.execute(new HttpGet(urls));
                        StatusLine statusLine = response.getStatusLine();
                        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                            ByteArrayOutputStream out = new ByteArrayOutputStream();
                            response.getEntity().writeTo(out);
                            out.close();
                            String responseString = out.toString();
                            //..more logic
                        } else{
                            //Closes the connection.
                            response.getEntity().getContent().close();
                            throw new IOException(statusLine.getReasonPhrase());
                        }
                    } catch (IOException e) {
                        Log.e("HTTP GET:", e.toString());
                    }

我得到了filenotfound的例外情况,或者当我得到外部代码的回复时我得到bad request,我搜索了这个问题并遇到了同样的问题并阅读了以下文章
C#/Java - Consuming WCF Service from Android emulator
Exposing a service running on localhost to an Android emulator?
How to fix android.os.NetworkOnMainThreadException?
并使用fiddler我在 JSON 中得到了正确的答案 任何帮助将不胜感激:)

0 个答案:

没有答案