Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

arrays - Part of JSON response from server is returning null: Android

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...