2012-09-10 56 views
0

无法连接android客户端与wcf服务。这是我的代码。WCF与Android客户端连接,无法连接Android客户端与WCF服务。这里是我的代码

@Override 
    public void onClick(View v) 
    { 
     switch(v.getId()) 
     { 
      case R.id.btnLogin: 
       String userName=txtUserName.getText().toString(); 
       String password=txtPassword.getText().toString(); 
       if(verifyLogin(userName,password)) 
       { 
        lblStatus.setText("Login Successful"); 
       } 
       else 
       { 
        //lblStatus.setText("Login Failed"); 
        lblStatus.setText("Login Failed"); 
       } 
       break; 
     } 
    } 





public static String convertStreamToString(InputStream is) 
    { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 

    public static boolean verifyLogin(String UserName,String Password) 
    { 
     try 
     { 
      DefaultHttpClient httpClient=new DefaultHttpClient(); 


HttpGet httpGet=new HttpGet("http://1.1.1.1/JSONSample/Service1.svc/checkLogin?name="+UserName+"&pass="+Password); 


HttpResponse httpResponse = httpClient.execute(httpGet); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      InputStream stream=httpEntity.getContent(); 

      String result= convertStreamToString(stream); 

      if(result.charAt(1)=='1') 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
     catch(Exception e) 
     { 
      return false; 
     } 

    } 


Also added internet permission. 

Below is my wcf service: 

IService1.cs 

[OperationContract] 
     [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "checkLogin?name={name}&pass={pass}")] 
     string checkLogin(string name, string pass); 


Service1.svc.cs 

public string checkLogin(string name, string pass) 
     { 
      DataAccess dataAccess = new DataAccess(); 
      return dataAccess.checkLogin(name, pass); 
     } 


DataAccess class : 

public class DataAccess 
    { 
     SqlConnection con; 
     public DataAccess() 
     { 
      con = new SqlConnection("Data Source=RILSWDIND105\\DEVELOPER;Initial Catalog=JSONSampleDB;user id=sa;[email protected]"); 
     } 

     public string checkLogin(string userName, string password) 
     { 
      if (con.State == ConnectionState.Closed) 
      { 
       con.Open(); 
      } 
      SqlCommand command = new SqlCommand("SELECT * FROM UserLogins where UserName='" + userName + "' AND Password='" + password + "'", con); 
      SqlDataReader reader = command.ExecuteReader(); 

      if (reader.Read()) 
      { 
       return "1"; 
      } 
      else 
      { 
       return "0"; 
      } 
     } 

    } 

Web.Config 

<system.serviceModel> 

    <behaviors> 
     <serviceBehaviors> 

     <behavior> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 

    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" minFreeMemoryPercentageToActivateService="0"/> 
    </system.serviceModel> 
<system.webServer> 
     <directoryBrowse enabled="true" /> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

WCF工作正常,当我测试与asp.net客户端是相同的,它是按照参数返回0 & 1。

当我运行我的android客户端时,它只是说,试图连接服务,但没有任何反应。

请帮助解决相同的问题。

回答

1

您是否正确使用端点?

这些链接将帮助您 Part1 Part2

+0

您好师先生,我已经使用我们的代码而已,我需要指定终点是什么:这样行吗? <服务名称= “JSONSample.Service1” behaviorConfiguration = “JSONSample.Service1Behavior”> <端点地址= “” 结合= “的WebHttpBinding” 合同= “JSONSample.IService1” behaviorConfiguration = “网络”> <端点地址= “MEX” 绑定= “mexHttpBinding” 合同= “IMetadataExchange接口”/> user1559414

+0

之后,你必须设置行为 HTTP:// guruparan.blog.com/files/2012/03/4.png –

+0

yess我做了完全一样的...服务得到编译,但在对话框(测试客户端)中没有添加服务。如果可能的话,我可以让你的邮件编号发送给我你的代码...可能会有总和愚蠢的错误,我无法指出它。 PLS。 – user1559414