0

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();
        }
    }
});
}
Naz141
  • 433
  • 1
  • 8
  • 31
  • 1
    http://stackoverflow.com/questions/29536233/deprecated-http-classes-android-lollipop-5-1 and https://developer.android.com/about/versions/android-5.1.html#http It could be due to the deprecation. If you have to use these you may need to add them to the gradle file explicitly. Add this in your android block useLibrary 'org.apache.http.legacy' – aminner Jan 26 '16 at 13:26
  • My emulator is also Android 5.1 it works. And it works on the device where i use the same code on the app for posting similar json data. Only for this contacts array it does not work on device. – Naz141 Jan 26 '16 at 13:33

0 Answers0