繁体   English   中英

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

[英]WCF Connectivity with android client, Unable to connect android client with wcf service. Here is my code

无法将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;pwd=km@1234");
        }

        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 client时,只是在说,尝试连接服务,但是什么也没有发生。

请帮助解决相同的问题。

您是否正确使用了端点?

这些链接将帮助您Part1 Part2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM