1

Hi I am trying to retrieve multiple rows of data from the database but whenever I run my app the app crashes.

In my case, I want to retrieve rows with the user name from the database.

   protected String doInBackground(String... args) {    
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            // getting JSON string from URL
            JSONObject json = jsonParser.
                    makeHttpRequest(url_all_coupons, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Products: ", json.toString());

            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                coupons = json.getJSONArray(TAG_COUPONS);

                // looping through All Products
                for (int i = 0; i < coupons.length(); i++) {
                    JSONObject c = coupons.getJSONObject(i);

                    // Storing each json item in variable
                    String couponcreated = c.getString(TAG_COUPONCREATED);
                    String couponexpires = c.getString(TAG_COUPONEXPIRES);
                    String coupondetails = c.getString(TAG_COUPONDETAILS);

This is my log cat

 E/JSON Parser﹕ Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

 3961-4003/info.androidhive.loginandregistration E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
            at java.util.concurrent.FutureTask.run(FutureTask.java:239)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.NullPointerException
            at info.androidhive.loginandregistration.CouponPageActivity$LoadAllCoupons.doInBackground(CouponPageActivity.java:98)
            at info.androidhive.loginandregistration.CouponPageActivity$LoadAllCoupons.doInBackground(CouponPageActivity.java:71)


            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)

This is my php code

$result = mysql_query("SELECT * FROM coupons WHERE name = '$name'") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["products"] = array();

while ($row = mysql_fetch_array($result)) {
    // temp user array
    $product = array();
    $product["couponcreated"] = $row["couponcreated"];
    $product["couponexpires"] = $row["couponexpires"];
    $product["coupondetails"] = $row["coupondetails"];


    // push single product into final response array
    array_push($response["products"], $product);
}
// success
$response["success"] = 1;
6
  • @Tan Chong Kai: I guess u r not parsing the Json response correctly. Initailly u need to get JSONObject that will be containing JSONArray of coupons. Mind if I see the JSON response. Commented Dec 17, 2015 at 5:57
  • @kevz i added my php code Commented Dec 17, 2015 at 6:02
  • @TanChongKai do you see your complete Json response properly as intended in this line Log.d("All Products: ", json.toString());? Can you post your json response you get in that line? Commented Dec 17, 2015 at 6:34
  • @Tan Chong Kai: I need to see JSON response string... Commented Dec 17, 2015 at 8:40
  • @kevz where can i find my json reponse? Commented Dec 18, 2015 at 2:27

1 Answer 1

2

you may be try this it will help you.

$result = mysql_query("SELECT * FROM coupons WHERE name = '$name'") or   die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();

while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["couponcreated"] = isset($row["couponcreated"])?$row["couponcreated"]:'';
$product["couponexpires"] = isset($row["couponexpires"])?$row["couponexpires"]:'';
$product["coupondetails"] = isset($row["coupondetails"])?$row["coupondetails"]:'';


// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.