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.