64866

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

Question:

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: <a href="https://stackoverflow.com/questions/29569640/org-json-jsonexception-value-of-type-java-lang-string-cannot-be-converted-to-js" rel="nofollow">org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray</a>

<a href="https://stackoverflow.com/questions/26870860/error-parsing-data-org-json-jsonexception-value-br-of-type-java-lang-string-ca" rel="nofollow">Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject</a>

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

Answer1:

<h2>Try this.....</h2> 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(); }

Answer2:

I observe your <em>json response</em>, 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 <strong>getString()</strong> instead of <strong>getJsonObject()</strong>

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

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

Recommend

  • Best Practice for variable with SQL in cfquery
  • ajaxform select2 concatenate multiple IDs
  • JSON Data Parsing Error the response from server becomes null: Android
  • 404 error - Google attempting to index ColdFusion CFC
  • How to automatically refresh the SO Order Entry Page
  • Set the initial selected option for rendered select in DataTables
  • Getting duplicate rows on left join in Birt reports
  • how to include a file inside a model in codeigniter?
  • How to set dynamic url for angular js
  • create table in pdf with wrap data using fpdf in php
  • Collect checkbox values in jQuery and POST them on submit
  • how to find common suffix in java by using method
  • Display java JPanel in a JFrame
  • onBackPressed() not being executed
  • iOS: Detect app start via notification press
  • how to adjust image in a panel in Java swing?
  • How to access EntityManager inside Entity class in EJB3
  • how to do an event when i swipe from fragment to the other
  • JFileChooser in front of fullscreen Swing application
  • HTML download movie download link
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • Trying to switch camera back to front but getting exception
  • Eraser for UIBezierPath
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Free memory of cv::Mat loaded using FileStorage API
  • Suggestions to manage Login/Logout transitions
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Programmatically clearing map cache
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?