Hi..
am developed one login form..dis is my coding part:
LoginLayoutActivity.java
package com.example.login;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LoginLayoutActivity extends Activity {
EditText un,pw;
TextView error;
Button ok;
protected static String username;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
un=(EditText)findViewById(R.id.et_un);
pw=(EditText)findViewById(R.id.et_pw);
ok=(Button)findViewById(R.id.btn_login);
error=(TextView)findViewById(R.id.tv_error);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
String response = null;
try {
response = CustomHttpClient.executeHttpPost("xxx/login3.php", postParameters);
String res=response.toString();
res= res.replaceAll("\\s+","");
if(res.equals("1"))
{
error.setText("Correct Username or Password");
Intent i = new Intent(getApplicationContext(),registerActivity.cl ass);
startActivity(i);
}
else
{
error.setText("Sorry!! Incorrect Username or Password");
}
} catch (Exception e) {
un.setText(e.toString());
}
}
});
}
}
registerActivity.java:
package com.example.login;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
public class registerActivity extends Activity {
/** Called when the activity is first created. */
TextView txt;
JSONObject json;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a crude view - this should really be set via the layout resources
// but since its an example saves declaring them in the XML.
LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);
// Set the text and call the connect function.
txt.setText("Connecting...");
//call the method to run the data retreival
txt.setText(getServerData(KEY_121));
}
public static final String KEY_121 = "xxx/lo.php"; //i use my real ip here
private String getServerData(String returnString) {
InputStream is = null;
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("usertype","A"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
Log.i("log_tag","Line reads: " + line);
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag",
"firstname: "+json_data.getString("firstname")+
"lastname: "+json_data.getString("lastname")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
}
login3.php:
<?php
// store session data
$un=$_POST['login'];
$pw=$_POST['password'];
//connect to the db
$user = 'root';
$pswd = '';
$db = 'xxx';
$conn = mysql_connect('localhost', $user, $pswd);
mysql_select_db($db, $conn);
//run the query to search for the username and password the match
$query = "SELECT firstname,lastname FROM customers WHERE login = '$un' AND password = '$pw' OR usertype ='A'";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
//this is where the actual verification happens
if(mysql_num_rows($result) > 0)
{
echo 1;
}
else
{
echo 0;
}
?>
lo.php:
<?php
$r=mysql_connect("localhost","root","");
mysql_select_db("xxx",$r);
$q=mysql_query("SELECT firstname,lastname FROM xcart_customers WHERE (usertype ='A')");
while($e=mysql_fetch_assoc($q))
{
$row[]=$e;
}
print(json_encode($row));
mysql_close()
?>
here lo.php am set usertype=A ...bez all admin user r logged in login page...that means admin users having permission... so here all admin user firstname and lastname displayed...but i need which admin person loged in login page that particular person detail only displayed...so plz give me some solutions...(i.e)krishnaveni,kannan,hariprasad r admin user means when krishnaveni loged means krishnaveni detail ly displayed...kannan logged means kannan details only displayed..i dono how it is do???? plz give me solutions....thanks in advance...
here easy to save the logged in username in a global variable .Then get it in the next activity and query again the database with this username..but i don't know how is to do...i checked
these links for setting the global variable and for
passing the extras in the intent.
but how is to do in my coding...plz help me...