JSON数据解析错误来自服务器的响应为空:Android [英] JSON Data Parsing Error the response from server becomes null: Android

查看:426
本文介绍了JSON数据解析错误来自服务器的响应为空:Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从服务器获取数据,然后将其显示到应用程序中。

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

我有JSON数据,

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

我有我的JAVA类,

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);

}

当我尝试记录服务器的响应时,我看到只有COLUMNS值存在,因为DATA为null。
另外,我收到如下错误,

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

为什么我要获取DATA值从服务器为null,我应该如何解决该错误?
我已经引用了链接: org.json.JSONException:类型java.lang.String的值不能转换为JSONArray

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

解析数据org.json.JSONException时出错:值<类型为java.lang.String的
无法转换为JSONObject

但是,这些对于我来说似乎没有帮助。

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

推荐答案

尝试一下.....


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();
            }

这篇关于JSON数据解析错误来自服务器的响应为空:Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆