{"id":6224,"date":"2014-04-13T23:31:21","date_gmt":"2014-04-13T23:31:21","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/android-parsing-json-returns-exception-collection-of-common-programming-errors\/"},"modified":"2014-04-13T23:31:21","modified_gmt":"2014-04-13T23:31:21","slug":"android-parsing-json-returns-exception-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/android-parsing-json-returns-exception-collection-of-common-programming-errors\/","title":{"rendered":"Android parsing json returns exception-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/12c87adce43676ceb553be58d90a96a7?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJanusz Chudzynski<\/p>\n<p>I am trying to parse a JSON using the JSONParser class presented below.<\/p>\n<p>Based on breakpoints it seems like it&#8217;s crashing on this line of code:<\/p>\n<p><strong><code>HttpResponse httpResponse = httpClient.execute(httpPost);<\/code><\/strong><\/p>\n<p>I don&#8217;t know how to proceed with it. I will appreciate any help.<\/p>\n<p><strong>FRAGMENT<\/strong><\/p>\n<pre><code> public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {\n        View view = inflater.inflate(R.layout.agenda, container, false);\n        final ListView listview = (ListView)view.findViewById(R.id.listView);\n        String[] values = new String[] {\"Summit 2013\",\"Sponsors\",\"Agenda\",\"Survey\",\"Website\",\"Credits\" };\n\n        Log.v(TAG,\"Created\");\n        String[] array = new String[10];\n        array[0] = \"\";\n\n        **new RetrieveFeedTask().doInBackground(\"\");**\n<\/code><\/pre>\n<p><strong>Async Task<\/strong><\/p>\n<pre><code>class RetrieveFeedTask extends AsyncTask {\n\n        private Exception exception;\n        protected JSONObject doInBackground(String... params) {\n            try {\n               \/\/ URL url= new URL(urls[0]);\n                JSONParser parser = new JSONParser();\n                JSONObject jobject = parser.getJSONFromUrl(\"http:\/\/www.itenwired.com\/json\");\n                return jobject;\n            } catch (Exception e) {\n\n                this.exception = e;\n                Log.v(TAG,\"Exception\"+exception.getLocalizedMessage());\n                return null;\n            }\n        }\n        protected void onPostExecute(JSONObject result) {\n            Log.v(TAG,\"Downloaded\");\n            Log.v(TAG,result.toString());\n        }\n    }\n<\/code><\/pre>\n<p><strong>JSON PARSER<\/strong><\/p>\n<pre><code>package com.iten.itenwired2013;\n\n\/**\n * Created by jmc72 on 7\/10\/13.\n *\/\nimport java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.InputStreamReader;\nimport java.io.UnsupportedEncodingException;\n\nimport org.apache.http.HttpEntity;\nimport org.apache.http.HttpResponse;\nimport org.apache.http.client.ClientProtocolException;\nimport org.apache.http.client.methods.HttpPost;\nimport org.apache.http.impl.client.DefaultHttpClient;\nimport org.json.JSONException;\nimport org.json.JSONObject;\nimport org.apache.http.client.HttpResponseException;\n\nimport android.util.Log;\n\npublic class JSONParser {\n    private static final String TAG = \"JSON PARSER\";\n\n    static InputStream is = null;\n    static JSONObject jObj = null;\n    static String json = \"\";\n\n    \/\/ constructor\n    public JSONParser() {\n\n    }\n\n    public JSONObject getJSONFromUrl(String url) {\n\n        \/\/ Making HTTP request\n        try {\n            \/\/ defaultHttpClient\n            DefaultHttpClient httpClient = new DefaultHttpClient();\n            HttpPost httpPost = new HttpPost(url);\n           \/\/ Log.v(TAG, httpPost.getURI().toString());\n           \/\/new HttpPost();\n            httpPost.setHeader(\"Content-type\", \"application\/json\");\n            Log.v(TAG, \"01\");\n            HttpResponse httpResponse = httpClient.execute(httpPost);\n            Log.v(TAG, \"Aaa \" + httpResponse.toString());\n            HttpEntity httpEntity = httpResponse.getEntity();\n            is = httpEntity.getContent();\n            Log.v(TAG, \"1\");\n\n        }\n        catch (HttpResponseException e) {\n            Log.v(TAG, \"000\");\n        }\n        catch (UnsupportedEncodingException e) {\n            Log.v(TAG, \"2\");\n            e.printStackTrace();\n        } catch (ClientProtocolException e) {\n            Log.v(TAG, \"3\");\n            e.printStackTrace();\n        } catch (IOException e) {\n            Log.v(TAG, \"4\");\n            e.printStackTrace();\n        }\n\n        try {\n            BufferedReader reader = new BufferedReader(new InputStreamReader(\n                    is, \"iso-8859-1\"), 8);\n            StringBuilder sb = new StringBuilder();\n            String line = null;\n            while ((line = reader.readLine()) != null) {\n                sb.append(line + \"\\n\");\n            }\n            is.close();\n            json = sb.toString();\n        } catch (Exception e) {\n            Log.e(\"Buffer Error\", \"Error converting result \" + e.toString());\n\n        }\n\n        \/\/ try parse the string to a JSON object\n        try {\n            jObj = new JSONObject(json);\n        } catch (JSONException e) {\n            Log.e(\"JSON Parser\", \"Error parsing data \" + e.toString());\n        }\n\n        \/\/ return JSON String\n        return jObj;\n\n    }\n}\n<\/code><\/pre>\n<p><strong>LOG CAT<\/strong><\/p>\n<pre><code>07-12 14:18:19.713      322-656\/system_process                 W\/ActivityManager: Unbind failed: could not find connection for android.os.BinderProxy@40d233c0\n07-12 14:18:20.953    1304-1304\/?                              D\/AndroidRuntime: &gt;&gt;&gt;&gt;&gt;&gt; AndroidRuntime START com.android.internal.os.RuntimeInit &gt; AndroidRuntime START com.android.internal.os.RuntimeInit<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Janusz Chudzynski I am trying to parse a JSON using the JSONParser class presented below. Based on breakpoints it seems like it&#8217;s crashing on this line of code: HttpResponse httpResponse = httpClient.execute(httpPost); I don&#8217;t know how to proceed with it. I will appreciate any help. FRAGMENT public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6224","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6224"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6224\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}