0

I am newbie to android Application. I made a login page where only already registered user from website can login. When user enters his username and password I want authenticate the registered user. How can I Authenticate username and password. I am using the following code please look at this and help me. when login button is clicked, it shows some errors.

Main.java

    public class Main extends Activity implements OnClickListener {

       Button ok,back,exit;
      TextView result;



     @Override
        public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   // Login button clicked
   ok = (Button)findViewById(R.id.btn_login);
   ok.setOnClickListener(this);

   result = (TextView)findViewById(R.id.lbl_result);

          }

         public void postLoginData() {
       // Create a new HttpClient and Post Header
       HttpClient httpclient = new DefaultHttpClient();

   /* login.php returns true if username and password is equal to saranga */
   HttpPost httppost = new HttpPost("http://yoursite.com/wp-login.php");

   try {
       // Add user name and password
    EditText uname = (EditText)findViewById(R.id.txt_username);
    String username = uname.getText().toString();

    EditText pword = (EditText)findViewById(R.id.txt_password);
    String password = pword.getText().toString();

       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
       nameValuePairs.add(new BasicNameValuePair("username", username));
       nameValuePairs.add(new BasicNameValuePair("password", password));
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

       // Execute HTTP Post Request
       Log.w("", "Execute HTTP Post Request");
       HttpResponse response = httpclient.execute(httppost);

       String str = inputStreamToString(response.getEntity().getContent()).toString();
       Log.w("", str);

       if(str.toString().equalsIgnoreCase("true"))
       {
        Log.w("", "TRUE");
        result.setText("Login successful");  
       }else
       {
        Log.w("", "FALSE");
        result.setText(str);            
       }

   } catch (ClientProtocolException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
    }

    private StringBuilder inputStreamToString(InputStream is) {
          String line = "";
           StringBuilder total = new StringBuilder();
           // Wrap a BufferedReader around the InputStream
             BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                    // Read response until the end
                  try {
               while ((line = rd.readLine()) != null) {
               total.append(line);
                }
              } catch (IOException e) {
              e.printStackTrace();
                       }
                // Return full string
             return total;
              }

              public void onClick(View view) {
                if(view == ok){
                 postLoginData();
                 }
                 }

                }

errors

 Execute HTTP Post Request
   Default buffer size used in BufferedReader constructor. It would be better to be       explicit if an 8k-char buffer is required.
  <html><head>    <title>yoursite.com: The Leading Website Site on the    Net</title>            <script type="text/javascript">;        if(self != top)    top.location.href = 'http://'+location.hostname+'/?redir=frame&  uid=yoursite50a47fe3380989.09080642';        </script>        <script   type="text/javascript" src="http://return.bs.domainnamesales.com  /return_js.php?d=yoursite.com&s=1352957923"></script>    </head>    <frameset cols="1,*,1"    border=0>        <frame name="top"   src="tg.php?uid=yoursite50a47fe3380989.09080642" scrolling=no frameborder=0 noresize   framespacing=0 marginwidth=0 marginheight=0>        <frame   src="search.php?uid=yoursite50a47fe3380989.09080642" scrolling="auto" framespacing=0  marginwidth=0 marginheight=0 noresize>        <frame   src="page.php?yoursite50a47fe3380989.09080642"></frame>    </frameset>      <noframes>        yoursite.com has been connecting our visitors with providers of Computer  Networking, Dedicated Web Servers, Email Domain Hosting and many other related  services for nearly 10 years. Join thousands of satisfied visitors who found Ftp Hosts, Ftp Servers, Host, Internet Servers, and Linux Servers.<br/>    </noframes></html>
FALSE
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
SRY
  • 139
  • 3
  • 15

2 Answers2

1

Try this Example .

http post method passing null values to the server

Here you have to send user name & password to web server and you will get response from server as per the request. so if use is reg or not that you have to check at service side.

I have use JSON to retrieve response .

Community
  • 1
  • 1
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
  • JSONObject json_data = new JSONObject(result);// its a string var which contain output. my_output_one = json_data.getString("var_1"); // its your response var form web. my_output_two = json_data.getString("var_2"); – SRY Nov 15 '12 at 06:37
  • The above example is closing the App – SRY Nov 15 '12 at 06:38
  • debug you app and check that data are sending to web or not otherwise may be problem in web service – Chintan Khetiya Nov 15 '12 at 07:01
0

If you actually check logcat properly you will find out that you have achieved what you wanted to achieve.

What you tried: Sending a HTTP POST request for login. What you got in reponse: The HTML page.

Your webapp does not return true/false for that particular HTTP POST request. Whereas you are checking for true.

Solution:
1. Parse HTML page to determine for successful login (NOT recommended)
2. Redesign you web app to return xml/json responses as well. That you you can have both web page and an API which you can use from any other app.

Other things you should take care of:
1. Never make network calls on UI thread. Might end up getting ANR
2. Never leave Log statements without TAGs. Tags makes debugging easier. You will understand this when you write huge chunks of code and deal with a lot complex problems than this one.

Hope this helps.

Abhishek Nandi
  • 4,265
  • 1
  • 30
  • 43