Manikandan Ramasamy
- Forum posts: 1
Jul 12, 2016, 1:05:17 PM via Website
Jul 12, 2016 1:05:17 PM via Website
I have trouble when I repeatedly make URL request. Android is crashing PHP. PHP comes back after a while normally but it's annoying. Here is the code. Can you please share what's the error I am making?
private class getRandomTeachingsOfTheWeek extends AsyncTask<Void, Void, String> {
String SERVER_URL = URLConstants.BASE_URL+URLConstants.GET_RANDOMTEACHINGSOFTHEWEEK;
@Override
protected String doInBackground(Void... params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(SERVER_URL);
HttpResponse response = client.execute(post);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
try {
Reader reader = new InputStreamReader(content);
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
List<TeachingsOfTheWeek> teachingsOfTheWeek = Arrays.asList(gson.fromJson(reader, TeachingsOfTheWeek[].class));
content.close();
handleTeachingsOfTheWeek(teachingsOfTheWeek);
} catch (Exception ex) {
Log.e(Constants.LOG_TAG, getResources().getString(R.string.ERROR_FAILED_TO_PARSE_JSON) + ex);
failedLoadingTeachingsOfTheWeek();
}
} else {
Log.e(Constants.LOG_TAG, getResources().getString(R.string.ERROR_SERVER_RESPONDED_WITH_STATUS_CODE) + statusLine.getStatusCode());
failedLoadingTeachingsOfTheWeek();
}
} catch(Exception ex) {
Log.e(Constants.LOG_TAG, getResources().getString(R.string.ERROR_FAILED_TO_SEND_HTTP_POST_REQUEST) + ex);
failedLoadingTeachingsOfTheWeek();
}
return null;
}
}
}
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.