0

cant show the total amount of this equation using php which is json encoded to a textview in android studio

here is the equation in php

$d=2;
$d0=(($b1*2*9)+($b2*3) + (4*$b3))-(($b1*4*3)+($b2*9)+(2*$b3));
$d1 = (($b1 * 4) + ($b2 * 9) + ($b3)) - (($b1 * 9) + ($b2) + (4 * $b3));
$d2 = (($b1 * 3) + ($b2) + (2 * $b3)) - (($b1 * 2) + ($b2 * 3) + ($b3));
$a0 = $d0 / $d;
$a1 = $d1 / $d;
$a2 = $d2 / $d;
$tot = $a0 + ($a1 * 4) + ($a2 * 16);
$format = new \stdClass();
$format->predicted=number_format($tot, 2);
$json = json_encode($format);
echo $json;

Note* $b1, $b2 and $b3 are data from mysql.

The output is this

{"predicted":"35,370,362.30"}

now the code in android studio which is getting the string predicted in json is this. the code is working on my other module tho. I think the php file has the problem her

public GetTextViewData(Context context)
    {
        this.context = context;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

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

        HttpClient myClient = new DefaultHttpClient();
        HttpPost myConnection = new HttpPost("http://10.0.2.2/sample/prediction.php");

        try {
            response = myClient.execute(myConnection);
            str = EntityUtils.toString(response.getEntity(), "UTF-8");

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


        try{
            JSONArray jArray = new JSONArray(str);
            json = jArray.getJSONObject(0);



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

        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(Void result)
    {
        try {
            tv1.setText(json.getString("predicted"));

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
}

this line is the error in my code

tv1.setText(json.getString("predicted"));
4
  • can you printout the complete response instead of trying to getString ? i would try that first to see if you are actually getting the json Commented Mar 20, 2018 at 19:31
  • W/System.err: org.json.JSONException: Value {"predicted":"35,370,362.30"} of type org.json.JSONObject cannot be converted to JSONArray Commented Mar 20, 2018 at 20:18
  • thats my error. Commented Mar 20, 2018 at 20:18
  • check this solution out stackoverflow.com/questions/12722468/… Commented Mar 21, 2018 at 13:04

0

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.