我尝试通过AsyncHttpClient将我的手机联系人JSON Array发布到服务器。我从设备中获取所有联系人并将其转换为json数组。调试后,我看到在Android设备上没有触发AsyncHttpClient。
以下代码适用于GenyMotion模拟器,但不适用于Android设备
在三星Note 4和A7上测试时,httpclient.post
无法执行。帖子在模拟器上工作
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();
}
}
});
}