Can anyone help me get my app connected to my database. I want a user
to enter information in the registration page and have that data
update and store in mysql database. For some reason though it is not
working. My exception error is being caught and saying my toast
message "can not connect" and no info is passed to the database. What
am I doing wrong?? Thanks in advance for any help

Here is my java

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class LoginJ extends Activity {
    Button login;
    String name="",pass="", EMAIL="";
    EditText username,password, email;
    TextView tv;
    byte[] data;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    InputStream inputStream;
    SharedPreferences app_preferences ;
    List<NameValuePair> nameValuePairs;
    CheckBox check;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        app_preferences =
PreferenceManager.getDefaultSharedPreferences(this);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        email = (EditText) findViewById(R.id.email);
        login = (Button) findViewById(R.id.login);
        check = (CheckBox) findViewById(R.id.check);

        String Str_user =
app_preferences.getString("username","0" );
        String Str_pass = app_preferences.getString("password",
"0");
        String Str_EMAIL = app_preferences.getString("email", "0");
        String Str_check = app_preferences.getString("checked",
"no");
        if(Str_check.equals("yes")) {
            username.setText(Str_user);
            password.setText(Str_pass);
            email.setText(Str_EMAIL);
            check.setChecked(true);

        }
        login.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v) {
                name = username.getText().toString();
                pass = password.getText().toString();
                EMAIL = email.getText().toString();
                String Str_check2 =
app_preferences.getString("checked", "no");
                if(Str_check2.equals("yes"))
                {
                    SharedPreferences.Editor editor =
app_preferences.edit();
                    editor.putString("username", name);
                   editor.putString("password", pass);
                   editor.putString("email", EMAIL);
                     editor.commit();

                }



                if(name.trim().equals("") || pass.trim().equals("") ||
EMAIL.trim().equals(""))
                {
                    Toast.makeText(LoginJ.this, "Blank Field..Please
Enter", Toast.LENGTH_LONG).show();
                }
                else{

                try{

                     httpclient = new DefaultHttpClient();
                                       httppost = new HttpPost("http://
10.0.2.2/AppConnection/main.php");

                                            // Add your data
                                            nameValuePairs = new
ArrayList<NameValuePair>(3);
                                           nameValuePairs.add(new
BasicNameValuePair("UserName", name.trim()));
                                            nameValuePairs.add(new
BasicNameValuePair("Password", pass.trim()));
                                            nameValuePairs.add(new
BasicNameValuePair("email", pass.trim()));
                                            httppost.setEntity(new
UrlEncodedFormEntity(nameValuePairs));

                                            // Execute HTTP Post
Request
                                            response =
httpclient.execute(httppost);
                                            inputStream =
response.getEntity().getContent();


                                            ResponseHandler<String>
responseHandler = new BasicResponseHandler();
                                            String response =
httpclient.execute(httppost, responseHandler);
 
System.out.println("Response : " +
response);
                                            // check the string from
the php side...
 
if(response.equalsIgnoreCase("Y")){
 
Toast.makeText(LoginJ.this, "login successfull",
Toast.LENGTH_LONG).show();
                                            }else{
 
Toast.makeText(LoginJ.this, "Cannot Log In",
Toast.LENGTH_LONG).show();
                                            }


                                            data = new byte[256];

                                            buffer = new
StringBuffer();
                                            int len = 0;
                                            while (-1 != (len =
inputStream.read(data)) )
                                            {
                                                buffer.append(new
String(data, 0, len));
                                            }
                                            inputStream.close();
                }

                catch (Exception e)
                {
                    Toast.makeText(LoginJ.this, "error"+e.toString(),
Toast.LENGTH_LONG).show();
                }
                if(buffer.charAt(0)=='Y')
                {
                    Toast.makeText(LoginJ.this, "login successfull",
Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(LoginJ.this, "Invalid Username or
password", Toast.LENGTH_LONG).show();
                }
                }
            }
        });
        check.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                // Perform action on clicks, depending on whether it's
now checked
                SharedPreferences.Editor editor =
app_preferences.edit();
                if (((CheckBox) v).isChecked())
                {

                     editor.putString("checked", "yes");
                     editor.commit();
                }
                else
                {
                     editor.putString("checked", "no");
                     editor.commit();
                }
            }
        });
    }
        public void Move_to_next()
        {

        startActivity(new Intent(this,
TheClassReviewActivity.class));
        }
    }



And here is my PHP


<?php
$hostname_localhost = "localhost";
$database_localhost = "logindatabase";
$username_localhost = "root";

$localhost = mysql_connect($hostname_localhost,$username_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);


?>

<?php
require_once('Connections/localhost.php');
mysql_select_db($database_localhost,$localhost);
$UserName = $_POST['UserName'];
$Password = $_POST['Password'];
$email = $_POST['email']

mysql_query("INSERT INTO tbl_users (UserName, Password, email) VALUES
('".$UserName."', '".$Password."', '".$email."')";
$query_exec = mysql_query($query_insert) or die (mysql_error());
$rows = mysql_num_rows($query_exec);

if($rows !==0) {echo "Y"};
else {echo "N"};
?>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to