android parsing json from URL
<p>this line is in onCreate function     </p>

<pre><code>JSONObject cat = getJSONfromURL("http://localhost/2010/hkinterview/index.php?op=androidCat");
</code></pre>

<p>The following code is the method which i found on web which parsing the json data from URL</p>

<pre><code>    public static JSONObject getJSONfromURL(String url){

    //initialize
    InputStream is = 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();

        is = entity.getContent();    

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection "+e.toString());   
    }
    //convert response to 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 the string to a JSON object

    try {
        jArray = new JSONObject(result);

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data "+e.toString());

    }
    return jArray;
}
</code></pre>

<p>The data output from the php </p>

<pre><code>[{"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&amp;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 &amp; R","img_cname":"nil"},{"cid":"20","cname":"\u92b7\u552e\u53ca\u5e02\u5834\u754c S &amp; 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"}]
</code></pre>

<p>the Errors</p>

<pre><code>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 at character 0 of 
</code></pre>

<p>the code seems it can't connect the url, any ideas?</p>

<p>updated:
after i added <code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;</code></p>

<pre><code>03-06 13:14:23.444: E/log_tag(1923): Error in http connection org.apache.http.conn.HttpHostConnectException: Connection to 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 at 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 with 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 to start activity ComponentInfo{com.me.hkinterview/com.me.hkinterview.hkinterviewActivity}: java.lang.NullPointerException
03-06 13:14:23.464: E/AndroidRuntime(1923):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at android.os.Looper.loop(Looper.java:123)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at java.lang.reflect.Method.invoke(Method.java:507)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at 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):     at com.me.hkinterview.hkinterviewActivity.onCreate(hkinterviewActivity.java:27)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-06 13:14:23.464: E/AndroidRuntime(1923):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-06 13:14:23.464: E/AndroidRuntime(1923):     ... 11 more
</code></pre>

<p>And i added real domain name</p>

<pre><code>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&amp;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 &amp; R"},{"img_cname":"nil","cid":"20","cname":"銷售及市場界 S &amp; 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 be converted to 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 with 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 to start activity ComponentInfo{com.me.hkinterview/com.me.hkinterview.hkinterviewActivity}: java.lang.NullPointerException
03-06 13:27:36.034: E/AndroidRuntime(2021):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at android.os.Looper.loop(Looper.java:123)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at java.lang.reflect.Method.invoke(Method.java:507)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at 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):     at com.me.hkinterview.hkinterviewActivity.onCreate(hkinterviewActivity.java:27)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-06 13:27:36.034: E/AndroidRuntime(2021):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-06 13:27:36.034: E/AndroidRuntime(2021):     ... 11 more
</code></pre>
