1

i am creating a simple test app for login process but the error that i get that when i use the browser in the emulator and try the url that i used it give me web page not available i used the localhost 127.0.0.2 but it give me the same msg and i used the ip of my pc the same msg anyone can help me ??

java code

package com.sencide;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidLogin extends Activity implements OnClickListener {


     Button ok,back,exit;
     TextView result;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

     // 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();
         Log.e("Responce-->","after httpclient");
        /* login.php returns true if username and password is equal to saranga */
        HttpPost httppost = new HttpPost("http://192.168.1.70/login.php");
        Log.e("Responce-->","after httppost");
        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));
            Log.e("Responce-->","after using the list name pair");

            // Execute HTTP Post Request
            Log.w("SENCIDE", "Execute HTTP Post Request");
            HttpResponse response = httpclient.execute(httppost);
            Log.e("Responce-->","after execute the http response");
          //  String str = inputStreamToString(response.getEntity().getContent()).toString();
            String str = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            //Log.w("SENCIDE", str);

            Log.e("Responce-->",""+str);

            if(str.toString().equalsIgnoreCase("true"))
            {
             Log.w("SENCIDE", "TRUE");
             result.setText("Login successful");  
            }else
            {
             Log.w("SENCIDE", "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;
       }

        /* login.php returns true if username and password is equal to saranga */


    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        if(view == ok){
             Thread t = new Thread(){
                    public void run(){
                        postLoginData();
                    }
                };
                t.start();

          }
    }





}

php code

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="testlogin"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
echo "true";
}
else {
echo "Login Failed";
}
?>
user2214618
  • 51
  • 1
  • 1
  • 12
  • 3
    localhost is 127.0.0.1 – Sammitch Apr 04 '13 at 17:51
  • What address are you using when you browse the login page on your desktop? Use that one on the emulator too. Also some information is missing (while other, like the java code, is overkill): does the emulator have proper connectivity? Are you using a firewall of sorts on your desktop? – Rick77 Apr 04 '13 at 17:54
  • Don't you need to use the 192.168.0.x (check ifconfig - or ipconfig on windows- to get the real machine IP) address? – Damien Pirsy Apr 04 '13 at 17:55
  • i think the localhost on the android emulator is 127.0.0.2 and i used the localhost on the pc and on the emulator but it did not work – user2214618 Apr 04 '13 at 17:58

1 Answers1

-1

Try hitting the php with a request from your php server using curl (if you are using a unix os) or make a simple web page to use. Make sure it's giving you a valid (200) response.

<html>
  <head>
  </head>
  <body>
    <form action="http://localhost/{{ your-php-script }}" method="POST">
      <input type="text" name="username">
      <input type="text" name="password">
      <input type="submit" value="submit">
    </form>
  </body>
</html>

Also, host pc should be 10.0.2.2, not 127.0.0.2 (or 127.0.0.1)

pnovotnak
  • 4,341
  • 2
  • 27
  • 38
  • it did not workk i changed the url to: **10.0.2.2, not 127.0.0.2 (or 127.0.0.1)** – user2214618 Apr 04 '13 at 18:35
  • What web server are you hosting the php with? It's hosted on the same box that you are using to run the emulator, right? – pnovotnak Apr 04 '13 at 18:46
  • it work i can get the page by entering 10.0.2.2 i tried before but it did not work but now i get an error msg in the log cat can you help me??[http://stackoverflow.com/questions/15819267/android-php-login-process-mysql] – user2214618 Apr 04 '13 at 18:55