Android: client-server, user authentication -
i have never built such android application communicate sever. want want send username , password server, match them on server , when username , password matches next screen should shown. next screen should have 1 text view saying "welcome username".
i want guys tell me step step guide -
- what should write in android app?
- what should write on server side?
- how , write server code?
- which language should use server side?
- how save several usernames-passwords on server?
i don't have real server. run entire code on localhost.
update:
this wrote in android app. don't know how correct.
public void clicked(view v) { system.out.println("button clicked"); edittext username = (edittext) findviewbyid(r.id.edituser); edittext password = (edittext) findviewbyid(r.id.editpass); editable user = username.gettext(); editable pass = password.gettext(); httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://192.168.1.101:8080//webapplication1/sendresponse.java"); try { // add data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("username", user.tostring())); namevaluepairs.add(new basicnamevaluepair("password", pass.tostring())); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); header[] headers = response.getallheaders(); int len = headers.length; for(int i=0; i<len; i++) { system.out.println("header : " + headers[i]); } inputstream inputstream = response.getentity().getcontent(); inputstreamreader inputstreamreader = new inputstreamreader(inputstream); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); stringbuilder stringbuilder = new stringbuilder(); string bufferedstrchunk = null; while((bufferedstrchunk = bufferedreader.readline()) != null){ stringbuilder.append(bufferedstrchunk); } system.out.println("response : " + stringbuilder.tostring()); } catch (clientprotocolexception e) { // todo auto-generated catch block } catch (ioexception e) { // todo auto-generated catch block } }
i don't know language best server side. should use rest on server side?
i'll authenticate using php. first of all,
httppost httppost = new httppost("http://10.0.2.2/login.php");
10.0.2.2 reffers localhost emulator. real device, need real server.
you have create db & table @ localhost using phpmyadmin.(assuming using wamp/lamp)
now, php file this:
<?php $link=mysql_connect('localhost','root','password');// give username & password if (!$link) { die('could not connect mysql: ' . mysql_error()); } mysql_select_db("database_name"); $sql=mysql_query("select * login_table_name user_name = '".$_request['username']."' , password = '".$_request['password']."'"); if (mysql_num_rows($sql) > 0) { echo "success"; }else{ echo "login failed"; } mysql_close($link); ?>
(assuming know put php file, localhost)(www folder wamp/lamp)
now, coming java, leave steps upto line is. , change code here to
httpresponse response = httpclient.execute(httppost); httpentity entity=response.getentity(); string res=entityutils.tostring(entity); if(res.contains("success")){ //login success } else{ //loginfailed }
now can how store usernames , passwords. yes, create registration form, wrap namevaluepairs , send php in sql query change 'insert into'.
nb: if run on emulator api-14+, exception- network on main thread exception. have use asynvtask. try in api-10.
edit:
if need values server, encode json , send. changes as,
php
if(mysql_num_rows($sql)>0){ while($row=mysql_fetch_assoc($sql)){// can use either fetch_assoc or fetch_array $result[]=$row; } echo json_encode($result); }
java
string res=entityutils.tostring(entity); log.d("tag","from server:"+res);// see server sent if( ! res.contains("login failed")){ jsonarray jarray = new jsonarray(res); if(jarray!=null){ for(int i=0;i<jarray.length();i++){ jsonobject jobj = jarray.getjsonobject(i); //now, catch values using column_name. as, id=jobj.getint("_id");name= jobj.getstring("name"); //note here _id,name column names. refer server result in logcat better understanding } } }
Comments
Post a Comment