0

Seriously, I have no idea what's was going wrong here.

This is my work_details table

enter image description here

Assume the ID is 3 , so I want to retrieve the timeIn and timeOut which twd belongs to 3 (In my code, I want to retrieve one row only).

  public void RetrieveTotalHours( final String ID) // ID(twd)=3
    {
        class GetHours extends AsyncTask<Void,Void,String> {
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(getActivity(),"Fetching...","Wait...",false,false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                showHours(s);
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequestParam(Configs.RETRIEVE_HOURS,ID);
                return s;
            }
        }
        GetHours ge = new GetHours();
        ge.execute();

    }
    private void showHours(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray result = jsonObject.getJSONArray(Configs.TAG_JSON_ARRAY);
            JSONObject c = result.getJSONObject(0);
            String MiNtimeIn = c.getString(Configs.TAG_IN); 
            String MaXtimeOut=c.getString(Configs.TAG_OUT);
            Log.e("min", MiNtimeIn);
            Log.e("max", MiNtimeOut);

    }

Retrieve_hours.php

 <?php
  define('HOST','127.0.0.1:3307');
  define('USER','root');
  define('PASS','');
  define('DB','androiddb');

  $con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect');

  $twd= $_GET['id'];

 $sql = "select * FROM work_details WHERE twd = '".$twd."'";

  $res = mysqli_query($con,$sql);

  $result=array();


  while($row=mysqli_fetch_array($res)){
      array_push($result,array('id'=>$row[0],'project'=>$row[1],'work_description'=>$row[2],'percentage'=>$row[3],'timeIn'=>$row[4],'timeOut'=>$row[5]));
  }

  echo (json_encode(array("result"=>$result)));

mysqli_close($con);

?>

I can see there are something display on the log . But when I specific the column, I will get this annoying error.

<?php
  define('HOST','127.0.0.1:3307');
  define('USER','root');
  define('PASS','');
  define('DB','androiddb');

  $con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect');

  $twd= $_GET['id'];

 $sql = "select timeIn , timeOut FROM work_details WHERE twd = '".$twd."'";

  $res = mysqli_query($con,$sql);

  $result=array();


  while($row=mysqli_fetch_array($res)){
      array_push($result,array('id'=>$row[0],'timeIn'=>$row[1],'timeOut'=>$row[2]));
  }

  echo (json_encode(array("result"=>$result)));

mysqli_close($con);

?>

Error

01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:159)
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:172)
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_WorkDetails.showHours(Edit_WorkDetails.java:248)
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_WorkDetails.access$000(Edit_WorkDetails.java:46)
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_WorkDetails$1GetHours.onPostExecute(Edit_WorkDetails.java:232)
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_WorkDetails$1GetHours.onPostExecute(Edit_WorkDetails.java:220)
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:632)
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at android.os.Looper.loop(Looper.java:146)
01-10 19:31:47.355    1298-1298/com.example.project.myapplication W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5602)
01-10 19:31:47.360    1298-1298/com.example.project.myapplication W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
01-10 19:31:47.360    1298-1298/com.example.project.myapplication W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
01-10 19:31:47.360    1298-1298/com.example.project.myapplication W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
01-10 19:31:47.360    1298-1298/com.example.project.myapplication W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
01-10 19:31:47.360    1298-1298/com.example.project.myapplication W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
01-10 19:31:47.380    1298-1298/com.example.project.myapplication D/AbsListView﹕ Get MotionRecognitionManager

Why value get display when I use *, but it gives me error when I specific the column name ?

John Joe
  • 12,412
  • 16
  • 70
  • 135
  • Is it possible that you are getting timeln as time object and you would need to explicitly convert it to string and then push it to json (common logic I do not know php)??? – elrado Jan 10 '16 at 14:14
  • @elrado then what should I do as my colume named timeIn – John Joe Jan 10 '16 at 14:19
  • HELO ANYONE CAN EXPLAIN THIS TO ME ? – John Joe Jan 10 '16 at 14:23
  • @JohnJoe a lot of people here are willing to help you, and this does look like a good question to me, but an all-caps _tell meh y meh codes not workz_ is not going to get you an answer any time sooner. – Nelewout Jan 10 '16 at 14:41
  • @N.Wouda Noted,no more next time :( – John Joe Jan 10 '16 at 14:43

2 Answers2

1

when mapping your result you are mapping the column $id which you didn't select in the select statement and probably that breaks at the server side and returns an HTML error message to the client which doesn't parse, fix the line:

array_push($result,array('id'=>$row[0],'timeIn'=>$row[1],'timeOut'=>$row[2]));

to:

array_push($result,array('timeIn'=>$row[0],'timeOut'=>$row[1]));
1

@joe,

Here is the issue:

Your query looks like this:

select timeIn , timeOut FROM work_details ....

You access the rows using index based array:

array('id'=>$row[0],'timeIn'=>$row[1],'timeOut'=>$row[2])

You fetching 3 columns instead of 2 you have selected in your SQL


I suggest you do not use numeric arrays when you fetch, because they rely on the position of the fields in your query. This will cause problems and bug if you ever change the query without paying attention at the fetching. You should at least use the associative arrays.

For example:

array('timeIn'=>$row['timeIn'],'timeOut'=>$row['timeOut'])

Even better You can check how I fetched the array in your previous question.

It saves you from bothering matching the fields

$res = mysqli_query($con,$sql);
$result=array();
while($row=mysqli_fetch_array($res)){
    $result[] = $row;
}
echo (json_encode(array("result"=>$result)));
Community
  • 1
  • 1
meda
  • 45,103
  • 14
  • 92
  • 122