-1

I am trying to fetch data from the server and then display it into the app. I have JSON data as,

{"COLUMNS":["TIMEID","BRANCHID","COMPANYID","MON_O","TUE_O","WED_O","THU_O","FRI_O","SAT_O","SUN_O","MON_C","TUE_C","WED_C","THU_C","FRI_C","SAT_C","SUN_C","CREATDATE"],"DATA":[[195,4,4,"09:00","09:00","09:00","09:00","09:00","Closed","Closed","16:30","16:30","16:30","16:30","16:30","Closed","Closed","May, 16 2017 08:16:12"]]}

When I access the url I get the complete JSON Data but when I am logging the same response from the server, I am not getting the DATA part of the JSON data.

My JAVA class implementation is as,

public static final String MON_O  = "MON_O";
public static final String TUE_O = "TUE_O";
public static final String WED_O = "WED_O";
public static final String THU_O = "THU_O";
public static final String FRI_O = "FRI_O";
public static final String SAT_O = "SAT_O";
public static final String SUN_O = "SUN_O";

public static final String MON_C = "MON_C";
public static final String TUE_C = "TUE_C";
public static final String WED_C = "WED_C";
public static final String THU_C = "THU_C";
public static final String FRI_C = "FRI_C";
public static final String SAT_C = "SAT_C";
public static final String SUN_C = "SUN_C";

public static final String JSON_ARRAY = "COLUMNS";

private void getData() {
String url =      context.getString(site_url)+"branch_time.cfc?method=branchtime&branchid=" +dBranchID;

StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {

    @Override
    public void onResponse(String response) {
        showJSON(response);
    }
},
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context,error.getMessage().toString(),Toast.LENGTH_LONG).show();
            }
        });

RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);
}

private void showJSON(String response) {

String name = "";

try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(JSON_ARRAY);
        Log.d("TAG", result.toString());

        name = result.getString(MON_O);
} catch (JSONException e) {
    e.printStackTrace();
}

 timeStatus.setText(name);
}

I am trying to log the result from the server and getting the below,

  ["TIMEID","BRANCHID","COMPANYID","MON_O","TUE_O","WED_O","THU_O","FRI_O","SAT_O","SUN_O","MON_C","TUE_C","WED_C","THU_C","FRI_C","SAT_C","SUN_C","CREATDATE"]

The DATA part of the response is null and I can get only the COLUMNS value. I have no idea why this could happen. Please can anyone help in this?

devgeek
  • 418
  • 3
  • 13
  • #try this https://stackoverflow.com/questions/44580161/json-data-parsing-error-the-response-from-server-becomes-null-android/44580614#44580614 – Rajasimman R Jun 17 '17 at 10:09

2 Answers2

1

Maybe it's null because you never did this to get that data correctly

JSONArray companyData = jsonObject.getJSONArray("DATA");

You only got the COLUMNS array, and the data is not within that array.

Also note that data array is an array within an array

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • When U do JSONArray companyData=jsonObject.getJSONArray("DATA"); Log.d("TAG", companyData.toString()); I returns null. – devgeek Jun 17 '17 at 08:32
  • returns null, or throws an exception? There's a large difference. You can use `optJSONArray()` if you are not guaranteed data – OneCricketeer Jun 17 '17 at 08:34
  • When I log the companyData I get D/TAG: [] – devgeek Jun 17 '17 at 08:35
  • Okay, that's an empty array... Not null – OneCricketeer Jun 17 '17 at 08:37
  • As I explained too, you have an array in an array. I assume those are rows of a table, in which case, I'd highly recommend rewriting the web API to give you more readable JSON and not keep as if it's still in a SQL database – OneCricketeer Jun 17 '17 at 08:40
  • ya sorry my mistake... so why I am I getting an empty array? Can u please suggest a way to use the same web API and not get the DATA empty? – devgeek Jun 17 '17 at 08:41
  • I can't access your API. My answer should be correct for the JSON you've provided – OneCricketeer Jun 17 '17 at 08:45
  • After following your answer, I still get the DATA array as empty and can't find any way to get the data. – devgeek Jun 17 '17 at 09:12
  • It works for me with the exact JSON you posted in the question – OneCricketeer Jun 17 '17 at 16:25
  • Yes I managed to work get the DATA but the issue is if I do MON_O = result.getString(0); I am getting the whole DATA as one complete string. – devgeek Jun 18 '17 at 02:55
  • I don't know what result is there, but `getString(0)` obviously isn't correct. You have an array within an array. If you called to get a String, of course you get that first list as an entire string – OneCricketeer Jun 18 '17 at 02:59
0

Code use for COLUMNS will be:

        JSONObject companyData = jsonObject.getJSONArray("COLUMNS");

Code use for DATA will be:

        JSONObject companyData = jsonObject.getJSONArray("DATA ");

Please mark my answer useful, if you get you desired solution.

Mr Code
  • 124
  • 10