title: Web service in Android using asynctask
body: <p>I have a class called "DataBase" extends "AsyncTask" for the use of remote database</p>
  
  <pre><code>public class DataBase extends AsyncTask&lt;String, Void, String&gt;{   
  
  ArrayList&lt;String&gt; arrayList = new ArrayList&lt;&gt;();
  String nameJson;
  
  public DataBase(String nameJson) {
      this.nameJson = nameJson;        
  }
  
  protected String doInBackground(String... params) {
  
      String jsonPhp = params[1];
  
          try {
              String line = null;
              URL url = new URL(jsonPhp);
              HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  
              InputStream inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
              BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
              StringBuffer stringBuffer = new StringBuffer();
  
              if(bufferedReader != null){
                  while((line = bufferedReader.readLine()) != null){
                      stringBuffer.append(line+" 'n");
                  }
              }else{
                  return null;
              }
  
              return stringBuffer.toString();
  
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
      return null;
  }
  
  protected void onPostExecute(String s) {
     arrayData(s);
  }
  
  public void arrayData(String buffer){
      try {
          JSONArray jsonArray = new JSONArray(buffer);
          JSONObject jsonObject = null;
          arrayList.clear();
  
          for(int i=0;i&lt;jsonArray.length();i++){
              jsonObject = jsonArray.getJSONObject(i);
              String dataList = jsonObject.getString(nameJson.toString());
              arrayList.add(dataList);
          }
      } catch (JSONException e) {
          e.printStackTrace();
      }
  }
  
  public ArrayList&lt;String&gt; getArrayList() {
      return arrayList;
    }
  
   }
  </code></pre>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>public class MainActivity {
  
  String adress = "http//....../.php"; //Here is the address of the file containing the json
  ListView listView;
  ArrayAdapter myArray;
  
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);        
      setContentView(R.layout.activity_main);
  
      listView = (ListView) findViewById(R.id.my_list);
      final DataBase dataBase = new DataBase("names");
      dataBase.execute(address); //This line of code is executed as if it were the last line of code, as if it were placed under the setAdapter.      
      myArray = dataBase.getArrayList(); // and for that reason this line return null
  
      ArrayAdapter arrayAdapter;
      arrayAdapter = new ArrayAdapter&lt;String&gt;(context,android.R.layout.simple_expandable_list_item_1,myArray);
      listView.setAdapter(arrayAdapter);
  
  
   }
  }
  </code></pre>
  
  <p>When I run my project in the line of code "dataBase.Execute ();" in the"MainActivity" class, does not execute the code block "doInBackground" immediately, but runs after finishes executing all lines of code that are in the "MainActivity", and for this reason in the line of code "myArray = dataBase.getArrayList ();" it returns null.</p>
  
  <p>I want to know how to make the "dataBase.Execute ();" run immediately and do not continue with the other lines of the "MainActivity" code, until it run the "doInBackground", and then the line of code" myArray = dataBase.getArrayList (); "no return null</p>