HttpURLConnection Android


I'm trying to get a list of users. I do this in a separate thread. Before that, I get a list of cities, also in a separate stream, and I get everything fine – the code is identical, only the request is different. This request works in the browser, but here it gives an error – at the end.

The site is out, so if you go to this address in the browser, then everything works.

private class GetDoctors extends AsyncTask<Void, Void, String> {

        HttpURLConnection conn = null;
        BufferedReader reader = null;
        String doctors = "";

        protected String doInBackground(Void... town_id) {
            // получаем данные с внешнего ресурса
            try {
                URL url = new URL("");
                Log.d("query", url.getQuery());
                conn = (HttpURLConnection) url.openConnection();

                InputStream inputStream = conn.getInputStream();
                StringBuffer buffer = new StringBuffer();

                reader = new BufferedReader(new InputStreamReader(inputStream));

                String line;

                while ((line = reader.readLine()) != null) {

                doctors = buffer.toString();

            } catch (Exception e) {

            return doctors;

        protected void onPostExecute(String doctors) {

            try {
                JSONObject doctorsObj = new JSONObject(doctors);

                Log.d("doctors", doctorsObj.toString());

                JSONArray townsArray =  new JSONArray(townsObj.getString("towns"));

                for (int i = 0; i < townsArray.length(); i++) {

                    JSONObject obj = new JSONObject(townsArray.getString(i));



            } catch (JSONException e) {


W/System.err:     at
W/System.err:     at$GetDoctors.doInBackground(
W/System.err:     at$GetDoctors.doInBackground(
W/System.err:     at android.os.AsyncTask$
W/System.err:     at
W/System.err:     at android.os.AsyncTask$SerialExecutor$
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$
W/System.err:     at
W/System.err: org.json.JSONException: End of input at character 0 of 
W/System.err:     at org.json.JSONTokener.syntaxError(
W/System.err:     at org.json.JSONTokener.nextValue(
W/System.err:     at org.json.JSONObject.<init>(
W/System.err:     at org.json.JSONObject.<init>(
W/System.err:     at$GetDoctors.onPostExecute(
W/System.err:     at$GetDoctors.onPostExecute(
W/System.err:     at android.os.AsyncTask.finish(
W/System.err:     at android.os.AsyncTask.access$600(
W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(
W/System.err:     at android.os.Handler.dispatchMessage(
W/System.err:     at android.os.Looper.loop(
W/System.err:     at
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(
W/System.err:     at$
W/System.err:     at


the second thread had to be started like this:

new GetDoctors().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Scroll to Top