无法将 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 客户端时,它只是说,试图连接服务,但没有任何反应。
请帮助解决同样的问题。