Android AsyncTask connection refuses emulator + Mamp-Collection of common programming errors
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 = "com.example.gestionformations.MESSAGE";
final static String URL = "http://10.0.2.2/apis/general/get_categories/";
final static String LOG = "gestionFormations.log";
ListView conferenceListe;
ListView coursListe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
client = new DefaultHttpClient();
Log.i(LOG,"Create");
new Read().execute("libcategorie");
}
public JSONObject getCategories() throws ClientProtocolException, IOException, JSONException{
StringBuilder url = new StringBuilder(URL);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if(status == 200){
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONArray catArray = new JSONArray(data);
JSONObject lesCategories = catArray.getJSONObject(0);
//Log.i(LOG,"JsonObject : "+ lesCategories.toString());
return lesCategories;
}else{
Log.i(LOG,"Erreur getCategories");
return null;
}
}
public class Read extends AsyncTask{
@Override
protected String doInBackground(String... params) {
try {
json = getCategories();
return json.getString(params[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Log.i(LOG,result);
}
}
}
My Manifest.xml looks like this :