android - Can someone help me with this JSON Parsing? -
there problem when retrieve individual elements the json. : "t":"sbin"
the json-url follows : http://finance.google.com/finance/info?q=bse:sbin
and other resources follows :
public class refreshstock extends asynctask<string, string, list<stockmodel>> { @override protected list<stockmodel> doinbackground(string... params) { try { url = new url(params[0]); connection = (httpurlconnection) url.openconnection(); connection.connect(); inputstream = connection.getinputstream(); bufferedreader = new bufferedreader(new inputstreamreader(inputstream)); stringbuffer = new stringbuffer(); string line = ""; while ((line = bufferedreader.readline()) != null) stringbuffer.append(line + "\n"); string data = stringbuffer.tostring(); return parsejson(data); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { if (connection != null) connection.disconnect(); try { if (bufferedreader != null) bufferedreader.close(); } catch (ioexception e) { e.printstacktrace(); } } return null; } private list<stockmodel> parsejson(string json) { list<stockmodel> stockmodellist = new arraylist<>(); json = json.substring(3); //textview.settext(""+json); try { stockmodel stockmodel = new stockmodel(); jsonobject parentobject = new jsonobject(); jsonarray jsonarray = parentobject.getjsonarray(json); jsonobject childobject = jsonarray.getjsonobject(0); string stockid = childobject.getstring("id"); string stockname = childobject.getstring("t"); string stockrate = childobject.getstring("l"); string stockchange = childobject.getstring("c"); string stockchangepercent = childobject.getstring("cp"); toast.maketext( getapplicationcontext(),""+stockrate, toast.length_long).show(); stockmodel.setid(stockid); stockmodel.sett(stockname); stockmodel.setl(stockrate); stockmodel.setc(stockchange); stockmodel.setcp(stockchangepercent); stockmodellist.add(stockmodel); return stockmodellist; } catch (jsonexception e) { e.printstacktrace(); } return stockmodellist; } protected void onpostexecute(list<stockmodel> result) { super.onpostexecute(result); stockadapter adapter = new stockadapter(getapplicationcontext(), r.layout.listrow, result); lvstock.setadapter(adapter); } }
the logcat message follows:
07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ org.json.jsonexception: no value [ 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ { 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ "id": "13564339" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"t" : "sbin" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"e" : "nse" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"l" : "222.90" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"l_fix" : "222.90" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"l_cur" : "rs.222.90" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"s": "0" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"ltt":"3:55pm gmt+5:30" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"lt" : "jul 4, 3:55pm gmt+5:30" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"lt_dts" : "2016-07-04t15:55:45z" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"c" : "+3.30" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"c_fix" : "3.30" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"cp" : "1.50" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"cp_fix" : "1.50" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"ccol" : "chg" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ,"pcls_fix" : "219.6" 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ } 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ ] 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ [ 07-04 10:49:42.667 11270:11321 w/system.err ] @ org.json.jsonobject.get(jsonobject.java:389) 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ @ org.json.jsonobject.getjsonarray(jsonobject.java:584) 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ @ org.example.km.http2json.mainactivity$refreshstock.parsejson(mainactivity.java:117) 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ @ org.example.km.http2json.mainactivity$refreshstock.doinbackground(mainactivity.java:87) 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ @ org.example.km.http2json.mainactivity$refreshstock.doinbackground(mainactivity.java:63) 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ @ android.os.asynctask$2.call(asynctask.java:288) 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ @ java.util.concurrent.futuretask.run(futuretask.java:237) 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 07-04 10:49:42.667 11270-11321/org.example.km.http2json w/system.err﹕ @ java.lang.thread.run(thread.java:818)
thanks :p
your json response not valid
replace
// [ { "id": "13564339" ,"t" : "sbin" ,"e" : "nse" ,"l" : "222.90" ,"l_fix" : "222.90" ,"l_cur" : "rs.222.90" ,"s": "0" ,"ltt":"3:55pm gmt+5:30" ,"lt" : "jul 4, 3:55pm gmt+5:30" ,"lt_dts" : "2016-07-04t15:55:45z" ,"c" : "+3.30" ,"c_fix" : "3.30" ,"cp" : "1.50" ,"cp_fix" : "1.50" ,"ccol" : "chg" ,"pcls_fix" : "219.6" } ] [ { "id": "13564339" ,"t" : "sbin" ,"e" : "nse" ,"l" : "222.90" ,"l_fix" : "222.90" ,"l_cur" : "rs.222.90" ,"s": "0" ,"ltt":"3:55pm gmt+5:30" ,"lt" : "jul 4, 3:55pm gmt+5:30" ,"lt_dts" : "2016-07-04t15:55:45z" ,"c" : "+3.30" ,"c_fix" : "3.30" ,"cp" : "1.50" ,"cp_fix" : "1.50" ,"ccol" : "chg" ,"pcls_fix" : "219.6" } ]
Comments
Post a Comment