I am try to POST my phone contacts JSON Array to server via AsyncHttpClient. i get all my contacts from the device and convert it to json array. After debugging i see the AsyncHttpClient does not trigger while on an android device.
The below code works on the GenyMotion Emulator but not on Android device
Tested on Samsung Note 4 and A7, the httpclient.post
does not execute. The post Works on Emulator
public void contactsToServer() throws JSONException, UnsupportedEncodingException {
JSONObject listContacts = new JSONObject();
try {
lstContacts.put("listContacts", JsonContactsArray);
} catch (JSONException e) {
e.printStackTrace();
}
String jsonContacts = listContacts.toString();
AsyncHttpClient httpClient = new AsyncHttpClient(true, 80, 443);
StringEntity entity = new StringEntity(jsonContacts);
httpClient.addHeader("hjtyu34", TOKEN);
httpClient.post(mycontext, URL, entity, "application/json", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
super.onSuccess(statusCode, headers, response);
System.out.println("onSuccess: " + response.toString());
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
if (statusCode == 404) {
Toast.makeText(getActivity().getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
} else if (statusCode == 500) {
Toast.makeText(getActivity().getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity().getApplicationContext(), "Server Down", Toast.LENGTH_LONG).show();
}
}
});
}