{"id":6223,"date":"2014-04-13T23:31:15","date_gmt":"2014-04-13T23:31:15","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/android-asynctask-connection-refuses-emulator-mamp-collection-of-common-programming-errors\/"},"modified":"2014-04-13T23:31:15","modified_gmt":"2014-04-13T23:31:15","slug":"android-asynctask-connection-refuses-emulator-mamp-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/android-asynctask-connection-refuses-emulator-mamp-collection-of-common-programming-errors\/","title":{"rendered":"Android AsyncTask connection refuses emulator + Mamp-Collection of common programming errors"},"content":{"rendered":"<p>I want to read JSon from a local web server in my android Application.<\/p>\n<p>This is my code :<\/p>\n<pre><code>public class MainActivity extends Activity implements OnItemClickListener {\n\nHttpClient client;\nJSONObject json;\n\npublic final static String EXTRA_MESSAGE = \"com.example.gestionformations.MESSAGE\";\nfinal static String URL = \"http:\/\/10.0.2.2\/apis\/general\/get_categories\/\";\nfinal static String LOG = \"gestionFormations.log\";\nListView conferenceListe;\nListView coursListe;\n\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_main);\n\n    client = new DefaultHttpClient();\n    Log.i(LOG,\"Create\");\n    new Read().execute(\"libcategorie\");\n}\n\npublic JSONObject getCategories() throws ClientProtocolException, IOException, JSONException{\n    StringBuilder url = new StringBuilder(URL);\n    HttpGet get = new HttpGet(url.toString());\n    HttpResponse r = client.execute(get);\n    int status = r.getStatusLine().getStatusCode();\n    if(status == 200){\n        HttpEntity e = r.getEntity();\n        String data = EntityUtils.toString(e);\n        JSONArray catArray = new JSONArray(data);\n        JSONObject lesCategories = catArray.getJSONObject(0);\n        \/\/Log.i(LOG,\"JsonObject : \"+ lesCategories.toString());\n        return lesCategories;\n    }else{\n        Log.i(LOG,\"Erreur getCategories\");\n        return null;\n    }\n}\n\npublic class Read extends AsyncTask{\n\n    @Override\n    protected String doInBackground(String... params) {\n        try {\n            json = getCategories();\n            return json.getString(params[0]);\n        } catch (ClientProtocolException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        } catch (IOException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        } catch (JSONException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        }\n        return null;\n    }\n\n    @Override\n    protected void onPostExecute(String result) {\n        \/\/ TODO Auto-generated method stub\n        Log.i(LOG,result);\n    }\n\n}\n}\n<\/code><\/pre>\n<p>My Manifest.xml looks like this :<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I want to read JSon from a local web server in my android Application. This is my code : public class MainActivity extends Activity implements OnItemClickListener { HttpClient client; JSONObject json; public final static String EXTRA_MESSAGE = &#8220;com.example.gestionformations.MESSAGE&#8221;; final static String URL = &#8220;http:\/\/10.0.2.2\/apis\/general\/get_categories\/&#8221;; final static String LOG = &#8220;gestionFormations.log&#8221;; ListView conferenceListe; ListView coursListe; @Override [&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-6223","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6223","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=6223"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6223\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}