java - Null response to php query in android, even though php works properly -
table
i'm trying retrieve above data using 2 php files below. want pass email parameter , return bets user = email. work fine when tested advanced rest client, problem not php. however, when try through android 2 java classes below, null pointer exception (logcat below) , json object returned null. i'm not sure error is, think i'm not passing parameter php , that's why it's producing null response.
logcat
05-31 15:10:50.227 26074-26074/com.example.albert.betterapp e/androidruntime﹕ fatal exception: main process: com.example.albert.betterapp, pid: 26074 java.lang.runtimeexception: unable start activity componentinfo{com.example.albert.betterapp/com.example.albert.betterapp.displayallbets}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2292) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2350) @ android.app.activitythread.access$800(activitythread.java:163) @ android.app.activitythread$h.handlemessage(activitythread.java:1257) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:157) @ android.app.activitythread.main(activitythread.java:5335) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.example.albert.betterapp.displayallbets.oncreate(displayallbets.java:79) @ android.app.activity.performcreate(activity.java:5389) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) @ android.app.activitythread.performlaunchactivity(activitythread.java:2256) at android.app.activitythread.handlelaunchactivity(activitythread.java:2350) at android.app.activitythread.access$800(activitythread.java:163) at android.app.activitythread$h.handlemessage(activitythread.java:1257) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:157) at android.app.activitythread.main(activitythread.java:5335) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:515) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) at com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) at dalvik.system.nativestart.main(native method)
get_bets.php
<?php class get_bets { private $db; function __construct() { require_once 'db_connect.php'; $this->db = new db_connect(); $response = array(); $this->db->connect(); } function __destruct() { } public function getusersbets($email) { $conn=mysqli_connect("****", "*****", "****","****"); $result = mysqli_query($conn,"select * bet user = '$email'"); $no_of_rows = mysqli_num_rows($result); if ($no_of_rows > 0) { $response["bet"] = array(); while ($row = mysqli_fetch_array($result)) { // temp user array $bet = array(); $bet["id"] = $row["id"]; $bet["stake"] = $row["stake"]; $bet["user"] = $row["user"]; $bet["returns"] = $row["returns"]; $bet["teams"] = $row["teams"]; $bet["status"] = $row["status"]; // push single gamelist final response array array_push($response["bet"], $bet); } return $response; } } } ?>
get_all_bets.php
<?php if (isset($_post['email']) && $_post['email'] != '') { // tag $email = $_post['email']; // include db handler require_once 'include/get_bets.php'; $db = new get_bets(); // response array $response = $db->getusersbets($email); echo json_encode($response); } ?>
displayallbets.java
public class displayallbets extends actionbaractivity { private string user1 = "user"; private static string url_all_games = "******"; // progress dialog private progressdialog pdialog; private string name; // creating json parser object jsonparser jparser = new jsonparser(); arraylist<hashmap<string, string>> bet; // url products list // json node names private static final string tag_success = "success"; private static final string tag_bet = "bet"; private static final string tag_id = "id"; private static final string tag_stake = "stake"; private static final string tag_user = "user"; private static final string tag_returns = "returns"; private static final string tag_teams = "teams"; private static final string tag_status = "status"; // products jsonarray jsonarray allgames = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_display_all_bets); name = (getintent().getextras().getstring("user")).tolowercase(); log.d("name",name); // hashmap listview bet = new arraylist<hashmap<string, string>>(); // loading products in background thread new loadallgames().execute(); /** * background async task load product making http request */ class loadallgames extends asynctask<string, string, string> { private string id; private string stake; private string user; private string returns; private string teams; private string status; // *//** // * before starting background thread show progress dialog // *//* @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(displayallbets.this); pdialog.setmessage("loading games. please wait..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } // *//** // * getting products url // *//* protected string doinbackground(string... args) { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("email", name)); // getting json string url jsonobject json = jparser.makehttprequest(url_all_games, "get", params); // check log cat json reponse try { // checking success tag int success = json.getint(tag_success); if (success == 1) { // products found // getting array of games string jsons = json.tostring(); log.d("json.tostring",jsons); string formattedjsons = jsons.substring(jsons.indexof('{')); log.d("formatted",formattedjsons); jsonobject jobj = new jsonobject(formattedjsons); allgames = jobj.getjsonarray(tag_bet); // looping through products (int = 0; < allgames.length(); i++) { jsonobject c = allgames.getjsonobject(i); // storing each json item in variable string id = c.getstring(tag_id); string user = c.getstring(tag_user); string returns = c.getstring(tag_returns); string stake = c.getstring(tag_stake); string status = c.getstring(tag_status); string teams = c.getstring(tag_teams); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put(tag_id, id); map.put(tag_teams, teams); map.put(tag_user, user); map.put(tag_returns, returns); map.put(tag_stake, stake); map.put(tag_status, status); // adding hashlist arraylist bet.add(map); } } } catch (jsonexception e) { e.printstacktrace(); } return ""; } /** * after completing background task dismiss progress dialog * * */ protected void onpostexecute(string file_url) { // dismiss dialog after getting products pdialog.dismiss(); // updating ui background thread } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_display_all_bets, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
jsonparser.java
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request try { // check request method if (method == "post") { // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } else if (method == "get") { // request method defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { if (!line.startswith("<", 0)) { if (!line.startswith("(", 0)) { sb.append(line + "\n"); } } } is.close(); json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
your java code performs http request:
jsonobject json = jparser.makehttprequest(url_all_games, "get", params);
and php code expects post request:
if (isset($_post['email']) && $_post['email'] != '')
you can change java code to:
jsonobject json = jparser.makehttprequest(url_all_games, "post", params);
or php code to:
if (isset($_get['email']) && $_get['email'] != '')
i'm not sure if problem.
Comments
Post a Comment