Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old January 30th, 2012, 08:24 AM   #1 (permalink)
Junior Member
 
Join Date: Dec 2011
Location: chennai
Posts: 61
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default login page user details fetched from database

Am developed one login form...here post the username and password from database using webservices...now its redirect to another activity,...that another activity having firstname and lastname for particular loged person details only fetched from database using json web services...
Because more number of users are there in admin side(ajay,krish,hari).... i need who r loged(ajay) in the login page that user(ajay) details only displayed …krish loged means krish details only displayed...but i done the some coding...

<?php
$r=mysql_connect("localhost","root","");
mysql_select_db("test",$r);

$q=mysql_query("SELECT firstname,lastname FROM customers WHERE usertype ='A'");

while($e=mysql_fetch_assoc($q))
{
$row[]=$e;
}
print(json_encode($row));



mysql_close()
?>
..here i set usertype=A...so all A user firstname and lastname details displayed...but i need only who is loged in login page that person firstname and lastname only displayed...

krishnaveni is offline  
Reply With Quote
Sponsors
Old January 30th, 2012, 09:12 AM   #2 (permalink)
Junior Member
 
jonathanrz's Avatar
 
Join Date: Jan 2012
Location: Brazil - Blumenau, SC
Posts: 69
 
Device(s): Motorola Defy - Gingerbread(2.3.3)
Thanks: 12
Thanked 5 Times in 4 Posts
Send a message via MSN to jonathanrz jonathanrafaelz
Default

You have to set in where the name of the person logged in.

$q=mysql_query("SELECT firstname,lastname FROM customers WHERE firstname =(name_of_user_logged)");
jonathanrz is offline  
Reply With Quote
Old January 30th, 2012, 09:50 PM   #3 (permalink)
Junior Member
 
Join Date: Dec 2011
Location: chennai
Posts: 61
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default

thanks for ur rly...already i set it...but it is not valuable coding....because ajay logged means ajay name only displayed...but i need all admin users logged in login page...its permission is give...which user loged means that particular user firstname and lastname only displayed...dis is my doubt...

here i am finished some 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...
krishnaveni is offline  
Reply With Quote
Old January 31st, 2012, 05:24 AM   #4 (permalink)
Junior Member
 
jonathanrz's Avatar
 
Join Date: Jan 2012
Location: Brazil - Blumenau, SC
Posts: 69
 
Device(s): Motorola Defy - Gingerbread(2.3.3)
Thanks: 12
Thanked 5 Times in 4 Posts
Send a message via MSN to jonathanrz jonathanrafaelz
Default

I understand, I think you could create a table of logged admins, or add a field in the user table with a flag Logged.

I don't know your structure, the best way to control sessions is with objects scope, if you could add same object in the server that is destroyed when the user log-off, you could search for the objects in the server to know who is logged.
jonathanrz is offline  
Reply With Quote
Old January 31st, 2012, 05:58 AM   #5 (permalink)
Junior Member
 
Join Date: Dec 2011
Location: chennai
Posts: 61
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default

s dis method is used in my project...but i dono session concepts...plz give me some link....then how it is used..
krishnaveni is offline  
Reply With Quote
Old January 31st, 2012, 06:11 AM   #6 (permalink)
Junior Member
 
Join Date: Dec 2011
Location: chennai
Posts: 61
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default

i used dis coding in mu LoginLayoutActivity.java:
HttpSession session = request.getSession(true);
HttpSession session = request.getSession (true);
if (session.isNew() == false) {
session.invalidate();
session = request.getSession(true);
}
session.setAttribute("username", firstname);
firstname username= (firstname )
session.getAttribute('username
');
if (username
== null) {
response.sendRedirect
("
LoginLayoutActivity");
return;
}

but i got error....invalid character constant in that pink color username..now how it is cleared...or any other solutions means give me...
krishnaveni is offline  
Reply With Quote
Old January 31st, 2012, 09:45 AM   #7 (permalink)
Junior Member
 
Join Date: Dec 2011
Location: chennai
Posts: 61
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default

In my loginlayoutactivity.java file have to set global variable ..plz how is set global variable...please help me...send me some useful link...i learned dis link http://orangic.com/android/android-global-variables-and-saving-application-state/...its useful link only..but in my simulator displayed force close message...badly not worked for me...how is set my coding part...give me some solutions...
krishnaveni is offline  
Reply With Quote
Old January 31st, 2012, 10:12 AM   #8 (permalink)
Junior Member
 
jonathanrz's Avatar
 
Join Date: Jan 2012
Location: Brazil - Blumenau, SC
Posts: 69
 
Device(s): Motorola Defy - Gingerbread(2.3.3)
Thanks: 12
Thanked 5 Times in 4 Posts
Send a message via MSN to jonathanrz jonathanrafaelz
Angry

Quote:
Originally Posted by krishnaveni View Post
session.getAttribute('username[/COLOR]');
if (username
== null) {
response.sendRedirect
("
LoginLayoutActivity");
return;
}
I already answered you that strings in java is surrounded by "".

Here is the post: Login page using sessions
jonathanrz is offline  
Reply With Quote
Old January 31st, 2012, 09:51 PM   #9 (permalink)
Junior Member
 
Join Date: Dec 2011
Location: chennai
Posts: 61
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default

k thanks for ur rly....how is declare global variable in android
krishnaveni is offline  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 12:11 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo