Reputation: 3675
I have been looking over my code all day and can not find why this is happening (or why its not actually returning the result form the MySQL server it should. Hopefully a fresh pair of eyes will find the reason.
The Jsonparser class
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is 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();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
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();
is = 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) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
The inner class within my activity
class GetUserDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LogIn.this);
pDialog.setMessage("Loading User details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// Building Parameters
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("email", EMAIL));
param.add(new BasicNameValuePair("password", PASSWORD));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject jsonuser = jsonParser.makeHttpRequest(
url_user_detials, "GET", param);
// check your log for json response
Log.d("User Details", jsonuser.toString());
// json success tag
try {
int success = jsonuser.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = jsonuser
.getJSONArray("product"); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("USERID", (Integer) product.get("ProfileID"));
editor.commit();
Intent intent = new Intent(LogIn.this, MyLists.class);
LogIn.this.startActivity(intent);
finish();
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
And finally the PHP script on the server talking with the database
<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET['email'] && isset($_GET['password')) {
$email = $_GET['email'];
$password = $_GET['password'];
// get a product from products table
$result = mysql_query("SELECT * FROM profiles_profiles WHERE ProfileEmail = '$email' AND ProfilePassword = '$password'");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["ProfileID"] = $result["ProfileID"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No user found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No user found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
and the stack trace for the actual error that I am getting
08-18 18:43:55.573: E/JSON Parser(16008): Error parsing data org.json.JSONException: End of input at character 0 of
08-18 18:43:55.573: W/dalvikvm(16008): threadid=13: thread exiting with uncaught exception (group=0x41515450)
08-18 18:43:55.584: E/AndroidRuntime(16008): FATAL EXCEPTION: AsyncTask #1
08-18 18:43:55.584: E/AndroidRuntime(16008): java.lang.RuntimeException: An error occured while executing doInBackground()
08-18 18:43:55.584: E/AndroidRuntime(16008): at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-18 18:43:55.584: E/AndroidRuntime(16008): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.lang.Thread.run(Thread.java:856)
08-18 18:43:55.584: E/AndroidRuntime(16008): Caused by: java.lang.NullPointerException
08-18 18:43:55.584: E/AndroidRuntime(16008): at com.test.app.LogIn$GetUserDetails.doInBackground(LogIn.java:149)
08-18 18:43:55.584: E/AndroidRuntime(16008): at com.test.app.LogIn$GetUserDetails.doInBackground(LogIn.java:1)
08-18 18:43:55.584: E/AndroidRuntime(16008): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-18 18:43:55.584: E/AndroidRuntime(16008): ... 5 more
and error from PHP
PHP Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')' in /home/sites/pmclient.co.uk/public_html/dadgapp/get_user_details.php on line 19
Upvotes: 1
Views: 10891
Reputation: 812
you forgot ";" in:
$password = $_GET['password']
line in your PHP code.
Upvotes: 1