JSON Data Parsing Error the response from server becomes null: Android

≡放荡痞女 提交于 2019-12-12 04:00:25

问题


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"]]}

I have my JAVA class 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);
        Log.d("TAG", jsonObject.toString());
        JSONArray result = jsonObject.getJSONArray(JSON_ARRAY);

            Log.d("TAG", result.toString());
            JSONObject companyData = result.getJSONObject(0);
            name = companyData.getString(MON_O);
            Log.e(name,"datadtadattadtatadtat");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    timeStatus.setText(name);

}

When I am triying to log the response from the server, I could see that only the COLUMNS value is there where as the DATA is null. Also, I am getting an error as below,

System.err: org.json.JSONException: Value TIMEID at 0 of type java.lang.String cannot be converted to JSONObject

Why would I get the DATA values from the server as null and how am I suppose to fix the error? I have referred the links: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray

Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

However, these do not seem to help in my case. Please can anyone help?


回答1:


Try this.....

JSONParser jsonParser = new JSONParser();
            jsonObject = jsonParser.getJSONFromUrl(url);

            try {
                user1 = jsonObject.getJSONArray("COLUMNS");

                for (int i = 0; i < user1.length(); i++) {
                    String value = (String) user1.get(i);

                    getList.add(value.toString());
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }



回答2:


I observe your json response, i think whatever you are having in column json array there are nothing having string which you are getting thats why you are getting error,

In your response nothing having key value pair so its easy to get String.

so you should getString() instead of getJsonObject()

private void showJSON(String response) {
    String name = "";
    try {
        JSONObject jsonObject = new JSONObject(response);
        Log.d("TAG", jsonObject.toString());
        JSONArray result = jsonObject.getJSONArray(JSON_ARRAY);
        Log.d("TAG", result.toString());
        name = result.getString(index);
        Log.e(name,"datadtadattadtatadtat");
    } catch (JSONException e) {
        e.printStackTrace();
    }

index is basically json array index you can put it 0 or something and go for loop if you want.



来源:https://stackoverflow.com/questions/44580161/json-data-parsing-error-the-response-from-server-becomes-null-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!