1

I have an issue with the code below. I parsed the JSON string from the database to the android but I can't seem to get the code running.

I used the asynctask class to doInBackground to retrieve the JSONparse from the database and gave it to onPostExecute method.

I'd like to give the parameters to the setCharges method to get the parameters and sets the charges to the android textview as double.

Please advice if anything is wrong with my code.

Thanks in advance.

The code below is the PHP script

<?php

require("config.php");

        //initial query
$query = "SELECT fHAH,sHAH,rHAH FROM charges";

//execute query

$stmt   = $db->prepare($query);
$result = $stmt->execute($query_params);

// Finally, we can retrieve all of the found rows into an array using fetchAll 
$rows = $stmt->fetchAll();


if ($rows) {

    $response["charges"] = array();

    foreach ($rows as $row) {

        $charge = array();

        $charge["fHAH"] = $row["fHAH"];
        $charge["sHAH"]    = $row["sHAH"];
        $charge["rHAH"]  = $row["rHAH"];


        //update our repsonse JSON data
        array_push($response["charges"], $post);
    }

    // echoing JSON response
    echo json_encode($response);


}

This is the AsyncTask Class

    private class getLoadData extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            JSONParser jParser = new JSONParser();
            json = jParser.getJSONDataFromURL(chargesSettingURL);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            double fCharge = 0.0;
            double sCharge = 0.0;
            double rCharge = 0.0;

            try {

                chargesJSONArray = json.getJSONArray(TAG_CHARGES);

                for (int i = 0; i < chargesJSONArray.length(); i++) {
                    JSONObject c = chargesJSONArray.getJSONObject(i);

                    fCharge = Double.parseDouble(c.getString(TAG_FHAH));
                    sCharge = Double.parseDouble(c.getString(TAG_SHAH));
                    rCharge = Double.parseDouble(c.getString(TAG_RHAH));


                    setCharges(fCharge, sCharge, rCharge);

                }

            }
            catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

And this is the setCharges method

public void setCharges(double fHAH, double sHAH, double rHAH) {
    this.F_HALF_AN_HOUR = fHAH;
    this.S_HALF_AN_HOUR = sHAH;
    this.R_HALF_AN_HOURS = rHAH;
}

I've included the error log

08-09 17:56:35.650    1860-2094/com.gpark.nerdherd.fyp 

E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.gpark.nerdherd.fyp, PID: 1860
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.StringBuilder.toString()' on a null object reference
            at com.gpark.nerdherd.fyp.libs.JSONParser.getJSONDataFromURL(JSONParser.java:45)
            at com.gpark.nerdherd.fyp.libs.CalculateParking$getLoadData.doInBackground(CalculateParking.java:42)
            at com.gpark.nerdherd.fyp.libs.CalculateParking$getLoadData.doInBackground(CalculateParking.java:36)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
08-09 17:56:43.620    2110-2110/com.gpark.nerdherd.fyp D/Atlas﹕ Validating map...
08-09 17:56:43.980    2110-2125/com.gpark.nerdherd.fyp D/﹕ HostConnection::get() New Host Connection established 0x7fd75984f1a0, tid 2125
08-09 17:56:44.000    2110-2125/com.gpark.nerdherd.fyp I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-09 17:56:44.060    2110-2125/com.gpark.nerdherd.fyp D/OpenGLRenderer﹕ Enabling debug mode 0
08-09 17:56:44.070    2110-2125/com.gpark.nerdherd.fyp W/EGL_emulation﹕ eglSurfaceAttrib not implemented
08-09 17:56:44.070    2110-2125/com.gpark.nerdherd.fyp W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x7fd75984e300, error=EGL_SUCCESS
08-09 17:56:47.150    2110-2129/com.gpark.nerdherd.fyp E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.gpark.nerdherd.fyp, PID: 2110
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.StringBuilder.toString()' on a null object reference
            at com.gpark.nerdherd.fyp.libs.JSONParser.getJSONDataFromURL(JSONParser.java:45)
            at com.gpark.nerdherd.fyp.libs.CalculateParking$getLoadData.doInBackground(CalculateParking.java:42)
            at com.gpark.nerdherd.fyp.libs.CalculateParking$getLoadData.doInBackground(CalculateParking.java:36)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
08-09 17:56:47.270    2110-2133/com.gpark.nerdherd.fyp I/Process﹕ Sending signal. PID: 2110 SIG: 9

This the the getJSONDataFromURL

public JSONObject getJSONDataFromURL(final String url) {

    try {

        urlObj = new URL(url);
        httpURLConnection = (HttpURLConnection) urlObj.openConnection();
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Accept-Charset", charset);
        httpURLConnection.setReadTimeout(10000);
        httpURLConnection.setConnectTimeout(15000);
        httpURLConnection.connect();
        paramsString = sbParams.toString();

        wr = new DataOutputStream(httpURLConnection.getOutputStream());
        wr.writeBytes(paramsString);
        wr.flush();
        wr.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        //Receive the response from the server
        InputStream in = new BufferedInputStream(httpURLConnection.getInputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "iso-8859-1"), 8);

        String line;
        while ((line = reader.readLine()) != null) {
            result.append(line);
        }

        in.close();
        json = result.toString();

    } catch (IOException e) {
        Log.e("JSON Parser", "result: " + e.toString());
    }

    httpURLConnection.disconnect();

    // 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 Object
    return jObj;

}
Jeff
  • 5,013
  • 1
  • 33
  • 30
developer5Urban
  • 43
  • 1
  • 12

1 Answers1

1

You seem to have invented a $post variable in your PHP code. Also you seem to be over complicating things

Try this

require("config.php");
//initial query
$query = "SELECT fHAH,sHAH,rHAH FROM charges";

$response = array();

// as there is nothing to prepare use ->query
foreach ($db->query($sql) as $row)

    $response["charges"][] = $row;
}

echo json_encode($response);

Now you have a proper json object to process in the java code on the android device

Now if you read this you will find out how to parse a JSON string into a JSON object in JAVA

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Have you changed the java code to parse the reply into a java object/array? – RiggsFolly Aug 09 '15 at 19:16
  • I send the JSON from the server to the JSONParser of the app to return a jsonObject. It's in the getJSONDataFromURL. I modified the getJSONDataFromURL to use HttpURLConnection instead of HttpClient, I am not sure if the getJSONDataFromURL has an error in the parsing, however it does say the method is producing a null reference. – developer5Urban Aug 10 '15 at 03:21