-3

I want to make constructor take url for use the class in many activity but it does not work.

There is my code:

class BackgroundTask extends AsyncTask<Void, Void, String> {


        String url1;
        //
        // there i want to take url from oncreate in main but not work
        //
        public BackgroundTask(String json_url) {
            super();
            this.url1=json_url;
        }


        @Override
        protected String doInBackground(Void... voids) {
            try {
                URL url =new URL(url1);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();
                while ((JSON_STRING_Doctor = bufferedReader.readLine()) != null) {
                    stringBuilder.append(JSON_STRING_Doctor + "\n");
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String res) {
            json_string = res;
        }


    }

and in oncreate I call first a new backgroudTask with url and after I call execute method but no result, json_string is null.

halfer
  • 19,824
  • 17
  • 99
  • 186
moe
  • 1
  • 3
    'i want to take url from oncreate in main but not work', what exactly is NOT working ? Post the exact exception – Vasu Dec 07 '16 at 23:28
  • Your constructor is perfectly fine. I'm guessing how you use the result of the AsyncTask is wrong. There's plenty of documentation out there, and I'd suggest you use a different networking library since AsyncTasks are all boilerplate code. http://stackoverflow.com/questions/16902716/comparison-of-android-networking-libraries-okhttp-retrofit-volley – OneCricketeer Dec 08 '16 at 14:51

1 Answers1

1

after I call execute method but no result, json_string is null.

Your constructor is fine, but this is not how you use an AsyncTask to get the result

task = new BackgroundTask(url);
task.execute();
// use json_string 

You need to wait for the response. The json string is never available before the call to onPostExecute, and even then, it could still be null because that's what you have within doInBackground when there's an exception.

Any code that requires the result of the task needs to be called following onPostExecute (so within that method, call other methods that take the json string), which should be easy to do if the AsyncTask is an inner class of where ever you need it.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245