php - When user logged in it fetches his data only -
i new android development , working on project in want display user data when logs in.
here's code after user logged in-:
public class userprofile extends activity { arrayadapter<string> adapt ; @override protected void oncreate(bundle savedinstancestate) { setcontentview(r.layout.list_view); adapt=new arrayadapter<string>(this,r.layout.list_view,r.id.textview1); profile pf = new profile(); pf.execute(); listview lv = (listview) findviewbyid(r.id.listview); lv.setadapter(adapt); super.oncreate(savedinstancestate); } class profile extends asynctask<void, void, string[]>{ dialog pd ; @override protected void onpreexecute() { super.onpreexecute(); pd = progressdialog.show(userprofile.this, "loading", "please wait..."); } @override protected string[] doinbackground(void... params) { list<namevaluepair> l = new arraylist<namevaluepair>(); intent intent = getintent(); string user = intent.getstringextra("user"); l.add(new basicnamevaluepair("username",user)); httpclient hc = new defaulthttpclient(); httppost post = new httppost("http://172.31.147.141/test/json.php"); stringbuilder sb = new stringbuilder(); string result = null; try { post.setentity(new urlencodedformentity(l)); httpresponse response = hc.execute(post); httpentity ent = response.getentity(); inputstream = ent.getcontent(); bufferedreader reader = new bufferedreader(new inputstreamreader(is, "utf-8"),8); while((reader.readline())!=null){ sb.append(reader.readline() + "\n"); } result = sb.tostring(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { return jsonfetch(result); } catch (jsonexception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(string[] result) { super.onpostexecute(result); pd.dismiss(); for(string ad : result){ adapt.add(ad); } } private string[] jsonfetch(string result) throws jsonexception{ final string comp_id ="comp_id"; string comp_type="comp_type"; //string availabletime="available_time"; string stat="stat"; string description="description"; string address ="address"; //string name="name"; string username="username"; //string com = "complaint"; jsonarray ja = new jsonarray(result); string[] resultstr = new string[ja.length()]; for(int = 0; i<ja.length();i++){ jsonobject obj = ja.getjsonobject(i); int compid = obj.getint(comp_id); string stats = obj.getstring(stat); string desc=obj.getstring(description); string addr=obj.getstring(address); //string nam = obj.getstring(name); string user = obj.getstring(username); string compty=obj.getstring(comp_type); resultstr[i] = "comp_id: "+compid+"\ncomp_type: "+compty+"\nstats:"+stats+"\naddress: "+addr+"\nname: "+user+"\ndescription : "+desc; } return resultstr; } }
}
and, here php code:-
<?php if($_server['request_method']=="post"){ $username='root'; $password=''; $server='localhost'; try{ $sqlcon = new pdo ("mysql: host=$server;dbname=project ims",$username,$password); $q1="select * f_c_r username = :user "; $sql=$sqlcon->prepare($q1); $sql->bindparam(":user",$_post['username']); $sql->execute(); $results=$sql->fetchall(); $json=json_encode($results); echo $json; } catch(pdoexception $e){ echo $e; } } ?>
when try run through , logcat shows
10-10 12:01:15.700: i/timeline(28017): timeline: activity_idle id: android.os.binderproxy@16a98ab1 time:68504901 10-10 12:01:16.596: e/spannablestringbuilder(28017): span_exclusive_exclusive spans cannot have 0 length 10-10 12:01:16.597: e/spannablestringbuilder(28017): span_exclusive_exclusive spans cannot have 0 length 10-10 12:01:19.556: i/timeline(28017): timeline: activity_idle id: android.os.binderproxy@16a98ab1 time:68508757 10-10 12:01:23.480: i/timeline(28017): timeline: activity_launch_request id:com.complaint time:68512682 10-10 12:01:23.552: d/androidruntime(28017): shutting down vm 10-10 12:01:23.554: e/androidruntime(28017): fatal exception: main 10-10 12:01:23.554: e/androidruntime(28017): process: com.complaint, pid: 28017 10-10 12:01:23.554: e/androidruntime(28017): java.lang.runtimeexception: unable start activity componentinfo{com.complaint/com.complaint.userprofile}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.listview.setadapter(android.widget.listadapter)' on null object reference 10-10 12:01:23.554: e/androidruntime(28017): @ android.app.activitythread.performlaunchactivity(activitythread.java:2356) 10-10 12:01:23.554: e/androidruntime(28017): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2418) 10-10 12:01:23.554: e/androidruntime(28017): @ android.app.activitythread.access$900(activitythread.java:154) 10-10 12:01:23.554: e/androidruntime(28017): @ android.app.activitythread$h.handlemessage(activitythread.java:1321) 10-10 12:01:23.554: e/androidruntime(28017): @ android.os.handler.dispatchmessage(handler.java:102) 10-10 12:01:23.554: e/androidruntime(28017): @ android.os.looper.loop(looper.java:135) 10-10 12:01:23.554: e/androidruntime(28017): @ android.app.activitythread.main(activitythread.java:5289) 10-10 12:01:23.554: e/androidruntime(28017): @ java.lang.reflect.method.invoke(native method) 10-10 12:01:23.554: e/androidruntime(28017): @ java.lang.reflect.method.invoke(method.java:372) 10-10 12:01:23.554: e/androidruntime(28017): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:904) 10-10 12:01:23.554: e/androidruntime(28017): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:699) 10-10 12:01:23.554: e/androidruntime(28017): caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.listview.setadapter(android.widget.listadapter)' on null object reference 10-10 12:01:23.554: e/androidruntime(28017): @ com.complaint.userprofile.oncreate(userprofile.java:42) 10-10 12:01:23.554: e/androidruntime(28017): @ android.app.activity.performcreate(activity.java:5990) 10-10 12:01:23.554: e/androidruntime(28017): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1106) 10-10 12:01:23.554: e/androidruntime(28017): @ android.app.activitythread.performlaunchactivity(activitythread.java:2309) 10-10 12:01:23.554: e/androidruntime(28017): ... 10 more 10-10 12:01:23.614: w/system.err(28017): org.json.jsonexception: value null of type org.json.jsonobject$1 cannot converted jsonarray 10-10 12:01:23.614: w/system.err(28017): @ org.json.json.typemismatch(json.java:111) 10-10 12:01:23.614: w/system.err(28017): @ org.json.jsonarray.(jsonarray.java:96) 10-10 12:01:23.614: w/system.err(28017): @ org.json.jsonarray.(jsonarray.java:108) 10-10 12:01:23.614: w/system.err(28017): @ com.complaint.userprofile$profile.jsonfetch(userprofile.java:114) 10-10 12:01:23.614: w/system.err(28017): @ com.complaint.userprofile$profile.doinbackground(userprofile.java:87) 10-10 12:01:23.614: w/system.err(28017): @ com.complaint.userprofile$profile.doinbackground(userprofile.java:1) 10-10 12:01:23.614: w/system.err(28017): @ android.os.asynctask$2.call(asynctask.java:292) 10-10 12:01:23.614: w/system.err(28017): @ java.util.concurrent.futuretask.run(futuretask.java:237) 10-10 12:01:23.614: w/system.err(28017): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 10-10 12:01:23.614: w/system.err(28017): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 10-10 12:01:23.614: w/system.err(28017): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 10-10 12:01:23.614: w/system.err(28017): @ java.lang.thread.run(thread.java:818)
.
Comments
Post a Comment