java - android parsing json from URL -


this line in oncreate function

jsonobject cat = getjsonfromurl("http://localhost/2010/hkinterview/index.php?op=androidcat"); 

the following code method found on web parsing json data url

    public static jsonobject getjsonfromurl(string url){      //initialize     inputstream = null;        string result = "";        jsonobject jarray = null;      //http post     try {          httpclient httpclient = new defaulthttpclient();            httppost httppost = new httppost(url);             httpresponse response = httpclient.execute(httppost);         httpentity entity = response.getentity();          = entity.getcontent();          } catch (exception e) {         log.e("log_tag", "error in http connection "+e.tostring());        }     //convert response string      try {          bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8);           stringbuilder sb = new stringbuilder();            string line = null;          while ((line = reader.readline()) != null) {                sb.append(line + "\n");         }          is.close();         result=sb.tostring();      } catch (exception e) {             log.e("log_tag", "error converting result "+e.tostring());     }     //try parse string json object      try {         jarray = new jsonobject(result);      } catch (jsonexception e) {         log.e("log_tag", "error parsing data "+e.tostring());      }     return jarray; } 

the data output php

[{"cid":"1","cname":"\u6703\u8a08\u754c accounting ","img_cname":"nil"},{"cid":"2","cname":"\u5ee3\u544a\u754c advertising","img_cname":"nil"},{"cid":"3","cname":"\u9280\u884c\u754c banking ","img_cname":"nil"},{"cid":"4","cname":"\u653f\u5e9c\u670d\u52d9\u754c civil serv","img_cname":"nil"},{"cid":"5","cname":"\u5efa\u7bc9\u754c construction","img_cname":"nil"},{"cid":"6","cname":"\u5ba2\u6236\u670d\u52d9\u696d cs ","img_cname":"nil"},{"cid":"7","cname":"\u8a2d\u8a08\u696d design","img_cname":"nil"},{"cid":"8","cname":"\u6559\u80b2\u754c","img_cname":"nil"},{"cid":"9","cname":"\u5de5\u7a0b\u754c engineering","img_cname":"nil"},{"cid":"10","cname":"\u98f2\u98df\u754c f&b ","img_cname":"nil"},{"cid":"11","cname":"\u9152\u5e97\u754c hotel ","img_cname":"nil"},{"cid":"12","cname":"\u4fdd\u96aa\u754c insurance ","img_cname":"nil"},{"cid":"13","cname":"\u6295\u8cc7\u754c investment ","img_cname":"nil"},{"cid":"14","cname":"\u6cd5\u5f8b\u754c law ","img_cname":"nil"},{"cid":"15","cname":"\u65b0\u805e\u50b3\u64ad\u754c media","img_cname":"nil"},{"cid":"16","cname":"\u91ab\u7642\u5065\u5eb7\u754c medicine\/health","img_cname":"nil"},{"cid":"17","cname":"\u63a1\u8cfc\u754c merchandising ","img_cname":"nil"},{"cid":"18","cname":"\u5730\u7522\u754c property","img_cname":"nil"},{"cid":"19","cname":"\u96f6\u552e\u98f2\u98df retail & r","img_cname":"nil"},{"cid":"20","cname":"\u92b7\u552e\u53ca\u5e02\u5834\u754c s & m","img_cname":"nil"},{"cid":"21","cname":"\u793e\u798f\u754c social services","img_cname":"nil"},{"cid":"22","cname":"\u96fb\u8a0a\u754c telecom","img_cname":"nil"},{"cid":"23","cname":"\u65c5\u904a\u696d tourism","img_cname":"nil"},{"cid":"24","cname":"\u5176\u4ed6 others","img_cname":"nil"},{"cid":"25","cname":"\u7269\u6d41\u754c logistics","img_cname":"nil"},{"cid":"26","cname":"\u8cc7\u8a0a\u79d1\u6280\u754c i.t.","img_cname":"nil"}] 

the errors

03-06 12:58:02.574: e/log_tag(1776): error in http connection java.net.socketexception: permission denied 03-06 12:58:02.574: e/log_tag(1776): error converting result java.lang.nullpointerexception 03-06 12:58:02.584: e/log_tag(1776): error parsing data org.json.jsonexception: end of input @ character 0 of  

the code seems can't connect url, ideas?

updated: after added <uses-permission android:name="android.permission.internet" />

03-06 13:14:23.444: e/log_tag(1923): error in http connection org.apache.http.conn.httphostconnectexception: connection http://localhost refused 03-06 13:14:23.444: e/log_tag(1923): error converting result java.lang.nullpointerexception 03-06 13:14:23.454: e/log_tag(1923): error parsing data org.json.jsonexception: end of input @ character 0 of  03-06 13:14:23.454: d/androidruntime(1923): shutting down vm 03-06 13:14:23.454: w/dalvikvm(1923): threadid=1: thread exiting uncaught exception (group=0x40015560) 03-06 13:14:23.464: e/androidruntime(1923): fatal exception: main 03-06 13:14:23.464: e/androidruntime(1923): java.lang.runtimeexception: unable start activity componentinfo{com.me.hkinterview/com.me.hkinterview.hkinterviewactivity}: java.lang.nullpointerexception 03-06 13:14:23.464: e/androidruntime(1923):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1647) 03-06 13:14:23.464: e/androidruntime(1923):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 03-06 13:14:23.464: e/androidruntime(1923):     @ android.app.activitythread.access$1500(activitythread.java:117) 03-06 13:14:23.464: e/androidruntime(1923):     @ android.app.activitythread$h.handlemessage(activitythread.java:931) 03-06 13:14:23.464: e/androidruntime(1923):     @ android.os.handler.dispatchmessage(handler.java:99) 03-06 13:14:23.464: e/androidruntime(1923):     @ android.os.looper.loop(looper.java:123) 03-06 13:14:23.464: e/androidruntime(1923):     @ android.app.activitythread.main(activitythread.java:3683) 03-06 13:14:23.464: e/androidruntime(1923):     @ java.lang.reflect.method.invokenative(native method) 03-06 13:14:23.464: e/androidruntime(1923):     @ java.lang.reflect.method.invoke(method.java:507) 03-06 13:14:23.464: e/androidruntime(1923):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 03-06 13:14:23.464: e/androidruntime(1923):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 03-06 13:14:23.464: e/androidruntime(1923):     @ dalvik.system.nativestart.main(native method) 03-06 13:14:23.464: e/androidruntime(1923): caused by: java.lang.nullpointerexception 03-06 13:14:23.464: e/androidruntime(1923):     @ com.me.hkinterview.hkinterviewactivity.oncreate(hkinterviewactivity.java:27) 03-06 13:14:23.464: e/androidruntime(1923):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 03-06 13:14:23.464: e/androidruntime(1923):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1611) 03-06 13:14:23.464: e/androidruntime(1923):     ... 11 more 

and added real domain name

03-06 13:27:36.024: e/log_tag(2021): error parsing data org.json.jsonexception: value [{"img_cname":"nil","cid":"1","cname":"會計界 accounting "},{"img_cname":"nil","cid":"2","cname":"廣告界 advertising"},{"img_cname":"nil","cid":"3","cname":"銀行界 banking "},{"img_cname":"nil","cid":"4","cname":"政府服務界 civil serv"},{"img_cname":"nil","cid":"5","cname":"建築界 construction"},{"img_cname":"nil","cid":"6","cname":"客戶服務業 cs "},{"img_cname":"nil","cid":"7","cname":"設計業 design"},{"img_cname":"nil","cid":"8","cname":"教育界"},{"img_cname":"nil","cid":"9","cname":"工程界 engineering"},{"img_cname":"nil","cid":"10","cname":"飲食界 f&b "},{"img_cname":"nil","cid":"11","cname":"酒店界 hotel "},{"img_cname":"nil","cid":"12","cname":"保險界 insurance "},{"img_cname":"nil","cid":"13","cname":"投資界 investment "},{"img_cname":"nil","cid":"14","cname":"法律界 law "},{"img_cname":"nil","cid":"15","cname":"新聞傳播界 media"},{"img_cname":"nil","cid":"16","cname":"醫療健康界 medicine\/health"},{"img_cname":"nil","cid":"17","cname":"採購界 merchandising "},{"img_cname":"nil","cid":"18","cname":"地產界 property"},{"img_cname":"nil","cid":"19","cname":"零售飲食 retail & r"},{"img_cname":"nil","cid":"20","cname":"銷售及市場界 s & m"},{"img_cname":"nil","cid":"21","cname":"社福界 social services"},{"img_cname":"nil","cid":"22","cname":"電訊界 telecom"},{"img_cname":"nil","cid":"23","cname":"旅遊業 tourism"},{"img_cname":"nil","cid":"24","cname":"其他 others"},{"img_cname":"nil","cid":"25","cname":"物流界 logistics"},{"img_cname":"nil","cid":"26","cname":"資訊科技界 i.t."}] of type org.json.jsonarray cannot converted jsonobject 03-06 13:27:36.024: d/androidruntime(2021): shutting down vm 03-06 13:27:36.024: w/dalvikvm(2021): threadid=1: thread exiting uncaught exception (group=0x40015560) 03-06 13:27:36.034: e/androidruntime(2021): fatal exception: main 03-06 13:27:36.034: e/androidruntime(2021): java.lang.runtimeexception: unable start activity componentinfo{com.me.hkinterview/com.me.hkinterview.hkinterviewactivity}: java.lang.nullpointerexception 03-06 13:27:36.034: e/androidruntime(2021):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1647) 03-06 13:27:36.034: e/androidruntime(2021):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 03-06 13:27:36.034: e/androidruntime(2021):     @ android.app.activitythread.access$1500(activitythread.java:117) 03-06 13:27:36.034: e/androidruntime(2021):     @ android.app.activitythread$h.handlemessage(activitythread.java:931) 03-06 13:27:36.034: e/androidruntime(2021):     @ android.os.handler.dispatchmessage(handler.java:99) 03-06 13:27:36.034: e/androidruntime(2021):     @ android.os.looper.loop(looper.java:123) 03-06 13:27:36.034: e/androidruntime(2021):     @ android.app.activitythread.main(activitythread.java:3683) 03-06 13:27:36.034: e/androidruntime(2021):     @ java.lang.reflect.method.invokenative(native method) 03-06 13:27:36.034: e/androidruntime(2021):     @ java.lang.reflect.method.invoke(method.java:507) 03-06 13:27:36.034: e/androidruntime(2021):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 03-06 13:27:36.034: e/androidruntime(2021):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 03-06 13:27:36.034: e/androidruntime(2021):     @ dalvik.system.nativestart.main(native method) 03-06 13:27:36.034: e/androidruntime(2021): caused by: java.lang.nullpointerexception 03-06 13:27:36.034: e/androidruntime(2021):     @ com.me.hkinterview.hkinterviewactivity.oncreate(hkinterviewactivity.java:27) 03-06 13:27:36.034: e/androidruntime(2021):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 03-06 13:27:36.034: e/androidruntime(2021):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1611) 03-06 13:27:36.034: e/androidruntime(2021):     ... 11 more 

for exception connection org.apache.http.conn.httphostconnectexception: connection http://localhost refused see error connection refused


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -