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クライアントを実行すると、サービスに接続しようとしているだけですが、何も起こりません。
同じ問題を解決するのを手伝ってください。