URL request from Android to PHP - crashes PHP after repeated times

  • Replies:1
  • OpenNot stickiedUnanswered
  • Forum posts: 1

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;
    }
}

}

  • Forum posts: 208

Jul 18, 2016 2:18:08 PM via Website

What crash you are getting . Share the Logs and Url as well in possible