Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Code are Here!
http://127.0.0.1:8083/MyService.svc/Login?UserName=admin&Password=ace[^]

this address retrun true.

C#
public void CheckUserNameandPwd() {
      // TODO Auto-generated method stub
      String  username = u.getText().toString().trim();
      String  pwd=pwdd.getText().toString().trim();
      if(username.length()==0){
          AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
          dlgAlert.setMessage("Fill UserName!");
          dlgAlert.setTitle("Error");
          dlgAlert.setPositiveButton("OK", null);
          dlgAlert.setCancelable(true);
          dlgAlert.create().show();
          return;
      }
      else if(pwd.length()==0 ){
          AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
          dlgAlert.setMessage("Fill Password!");
          dlgAlert.setTitle("Error");
          dlgAlert.setPositiveButton("OK", null);
          dlgAlert.setCancelable(true);
          dlgAlert.create().show();
          return;
      }
      else{
          if(verifyLogin(username,pwd)){
              Toast.makeText(getApplicationContext(),"Login Success",Toast.LENGTH_SHORT).show();
              startActivity(new Intent(Login.this,MainActivity.class));
          }else{
              Toast.makeText(getApplicationContext(),"error occur!",Toast.LENGTH_SHORT).show();
             // startActivity(new Intent(Login.this,mainlayout_activity.class));
          }
      }
  }

  private boolean verifyLogin(String username, String pwd) {
      try{
          showpDialog();
          DefaultHttpClient httpclient=new DefaultHttpClient();
          HttpGet httpget=new HttpGet("http://127.0.0.1:8083/MyService.svc/LoginForUsers?UserName=" + username + "&Password=" + pwd);
          HttpResponse httpresponse=httpclient.execute(httpget);
          HttpEntity httpentity=httpresponse.getEntity();
          InputStream stream=httpentity.getContent();
          String result=ConvertStreamToString(stream);
          if(result.charAt(1)=='1'){
              hidepDialog();
              return  true;
          }else {
              hidepDialog();
              return  false;
          }
      }catch (Exception e){
          hidepDialog();
          return  false;

      }
  }

  private String ConvertStreamToString(InputStream is) {
      BufferedReader reader=new BufferedReader(new InputStreamReader(is));
      StringBuffer sb=new StringBuffer();
     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();
  }

............
but i face this error msg in logcat.
Quote:
username={java.long.String@3773}"admin"
pwd={java.long.String@3775}"ace"
e={android.os.NetworkOnMainThreadException@3806}" android.os.NetworkOnMainThreadExcetpion
cause="android.os.NetworkOnMainThreadExcetpion"
detailMessage=null
staickState={java.lang.Object[32]@3829}
StackTrace={java.lang.StackTraceElement[0]@3830
Posted
Updated 16-Mar-15 0:13am
v2

1 solution

@SuppressLint("NewApi")
	public boolean isValid(String UName,String Pswd)
	{
		
		 String msg="";
		
		try {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();				
		    StrictMode.setThreadPolicy(policy);
		    
		    DefaultHttpClient httpClient = new DefaultHttpClient();
		    URI uri = new URI("http://10.0.2.2:13514/Service1.svc/IsValidUser?userid="+UName+"&password="+Pswd); 
		 
		    HttpGet httpget = new HttpGet(uri);
/*		    httpget.setHeader("Accept", "application/json");
		    httpget.setHeader("Content-type", "application/json; charset=utf-8");*/
		 
		    HttpResponse response = httpClient.execute(httpget);
		    HttpEntity entity = response.getEntity();
				BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()), 65728);
			      msg = in.readLine();
			      if(msg.contains("success")){

			    	  return true;
			      }
			      else
			      {

			    	  return false;
			      }
		}
		catch(Exception ex)
		{
					Log.v("Tag","error"+ex.getMessage());

					return false;
		}
		
	}


and Dont forget to add
<uses-permission android:name="android.permission.INTERNET" />

in Manifeast file
 
Share this answer
 
v2
Comments
MgKyaing 16-Mar-15 8:12am    
ys,sir...i have been added that permision in my project.sir..not forget.sir.but not Ok.sir. why?
Sid_Joshi 18-Mar-15 3:33am    
That's good practice.
MgKyaing 18-Mar-15 23:19pm    
ys,sir.
i face this error when connect android client to asp.net IIS Server.

errorConnection to http://127.0.0.1:8083 refused

link of IIS server is:http://127.0.0.1:8083/MyService.svc/Login?UserName=admin&Password=ace
this link returns me "true" value.Sir.
why?help me plz.
Sid_Joshi 19-Mar-15 1:40am    
127.0.0.1 is the device's own loopback interface. to access development machine's localhost use 10.0.2.2.
MgKyaing 24-Mar-15 9:54am    
ys,sir
my project is run on emulator device .sir
i face error connection refused error when running my project .sis.
do u need to config in my emulator for ip address or iis server's address.
if so,how do i fix that error.sir.
plz help me.sir .Thanks.a lot.sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900