android - displaying jsonarray data on textview -
im trying make android application school. want happen first, when user loggedin username saved in sharedpreferences second, using sharedpreference retrieve user info in database. third, parse data , display info in textviews. php file
<?php /* our "config.inc.php" file connects database every time include or require within php script. since want script add new user our db, talking our database, , therefore, let's require connection happen: */ require("config.inc.php"); if (!empty($_post)) { //initial query $query = "select * userinfo username = :user"; $query_params = array(':user' = > $_post['username']); //execute query try { $stmt = $db - > prepare($query); $result = $stmt - > execute($query_params); } catch (pdoexception $ex) { $response["success"] = 0; $response["message"] = "database error!"; die(json_encode($response)); } // finally, can retrieve of found rows array using fetchall $rows = $stmt - > fetchall(); if ($rows) { $response["success"] = 1; $response["message"] = "post available!"; $response["users"] = array(); foreach($rows $row) { $user = array(); $user["username"] = $row["username"]; $user["firstname"] = $row["firstname"]; $user["middlename"] = $row["middlename"]; $user["lastname"] = $row["lastname"]; $user["course"] = $row["course"]; //update our repsonse json data array_push($response["users"], $user); } // echoing json response echo json_encode($response); } else { $response["success"] = 0; $response["message"] = "no user available!"; die(json_encode($response)); } } else {} ?>
and java code
import java.util.arraylist; import java.util.list; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.app.progressdialog; import android.content.sharedpreferences; import android.os.asynctask; import android.os.bundle; import android.preference.preferencemanager; import android.util.log; import android.widget.textview; public class profileactivity extends activity { // xml labels textview txtfname; textview txtmname; textview txtlname; // progress dialog private progressdialog pdialog; // creating json parser object jsonparser jsonparser = new jsonparser(); // profile json object jsonarray user; jsonobject hay; // profile json url private static final string profile_url = "http://10.0.2.2/webservice1/userprofile.php"; // json node names private static final string tag_profile = "user"; // private static final string tag_id = "id"; private static final string tag_username = "username"; private static final string tag_firstname = "firstname"; private static final string tag_middlename = "middlename"; private static final string tag_lastname = "lastname"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_profile); txtfname = (textview) findviewbyid(r.id.fname); txtmname = (textview) findviewbyid(r.id.mname); txtlname = (textview) findviewbyid(r.id.lname); // loading profile in background thread new loadprofile().execute(); } /** * background async task load profile making http request * */ class loadprofile extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ public void test(){ /** * updating parsed json data listview * */ // storing each json item in variable try { string firstname = hay.getstring(tag_firstname); string middlename = hay.getstring(tag_middlename); string lastname = hay.getstring(tag_lastname); // displaying data in textview txtemail.settext("email: " + firstname); txtmobile.settext("mobile: " + middlename); txtaddress.settext("add: " + lastname); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(profileactivity.this); pdialog.setmessage("loading profile ..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } /** * getting profile json * */ protected string doinbackground(string... args) { // building parameters sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(profileactivity.this); string post_username = sp.getstring("username", "anon"); list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("username", post_username)); // getting json string url jsonobject json = jsonparser.makehttprequest(profile_url, "post", params); // check log cat json reponse log.d("profile json: ", json.tostring()); try { // profile json object user = json.getjsonarray(tag_profile); } catch (jsonexception e) { e.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog after getting products pdialog.dismiss(); // updating ui background thread test(); } } }
im getting nullpointer exception profileactivity$loadprofile.test please me im beginner
you have not initialized hey. have declaired hey. if data in json
assign hey = json in postexecute or pass json directly test , use there.
Comments
Post a Comment