{"id":6744,"date":"2014-04-23T02:03:11","date_gmt":"2014-04-23T02:03:11","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/23\/android-fatal-connection-with-json-collection-of-common-programming-errors\/"},"modified":"2014-04-23T02:03:11","modified_gmt":"2014-04-23T02:03:11","slug":"android-fatal-connection-with-json-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/23\/android-fatal-connection-with-json-collection-of-common-programming-errors\/","title":{"rendered":"Android Fatal Connection with JSON-Collection of common programming errors"},"content":{"rendered":"<p>I am having issues with my attempt to connect via JSON to a PHP file to access MySQL in android. I cannot for the life of me figure out why this is causing my app to crash. Any ideas?<\/p>\n<p>It is throwing the following error:<\/p>\n<pre><code>AndroidRuntime\nFATAL EXCEPTION: main\n<\/code><\/pre>\n<p>I am attemping to call this from my main activity:<\/p>\n<pre><code>new getData().getMovie();\n<\/code><\/pre>\n<p>Which calls this:<\/p>\n<pre><code>import java.io.BufferedReader;\nimport java.io.InputStream;\nimport java.io.InputStreamReader;\nimport java.util.ArrayList;\n\nimport org.apache.http.*;\nimport org.json.*;\n\n\nimport android.util.Log;\nimport android.widget.Toast;\n\npublic class getData extends FullscreenActivity{\n\npublic void getMovie() {\n    String result = \"\";\n    String rID=\"1\";\n    \/\/the year data to send\n    ArrayList nameValuePairs = new ArrayList();\n    nameValuePairs.add(new BasicNameValuePair(\"ids\",rID));\n\n    \/\/http post\n    try{\n            HttpClient httpclient = new DefaultHttpClient();\n            HttpPost httppost = new HttpPost(\"http:\/\/website.com\/myphp.php\");\n            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));\n            HttpResponse response = httpclient.execute(httppost);\n            HttpEntity entity = response.getEntity();\n            InputStream is = entity.getContent();\n    }catch(Exception e){\n            Toast.makeText(this, \"Error in http connection \"+e.toString(), Toast.LENGTH_LONG).show();\n            \/\/Log.e(\"log_tag\", \"Error in http connection \"+e.toString());\n    }\n    \/\/convert response to string\n    try{\n            InputStream is = null;\n            BufferedReader reader = new BufferedReader(new InputStreamReader(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\n            result=sb.toString();\n    }catch(Exception e){\n            Toast.makeText(this, \"Error converting result \"+e.toString(), Toast.LENGTH_LONG).show();\n            \/\/Log.e(\"log_tag\", \"Error converting result \"+e.toString());\n    }\n\n    \/\/parse json data\n    try{\n            JSONArray jArray = new JSONArray(result);\n            for(int i=0;i<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I am having issues with my attempt to connect via JSON to a PHP file to access MySQL in android. I cannot for the life of me figure out why this is causing my app to crash. Any ideas? It is throwing the following error: AndroidRuntime FATAL EXCEPTION: main I am attemping to call this [&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-6744","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6744","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=6744"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6744\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}