title: Too much time consumed to retrieve data from server
body: <p>I am developing an app that retrieves data from a php server. I am receiving a json array from the php server. When I run apk in android emulator, it works perfectly, but the problem occurs when I run this apk on real android phone. It takes too much time to retrieve data. I have searched a lot of resources, but I am unable to solve this problem. Please any one give me some hints to solve this problem. Thanks in advance.</p>
  
  <p>CostForSecretCloseMem class</p>
  
  <pre><code>public class CostForSecretCloseMem extends AppCompatActivity
  {
  
          private ListView listView;
          private RelativeLayout emptyView;
  
          @Override
          protected void onCreate(@Nullable Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.costs_layout);
                  initComponent();
          }
  
          @SuppressLint("SetTextI18n")
          private void initComponent()
          {
                  emptyView = findViewById(R.id.empty_view);
                  SharedPreferenceData sharedPreferenceData = new SharedPreferenceData(this);
                  CheckInternetIsOn internetIsOn = new CheckInternetIsOn(this);
                  listView = findViewById(R.id.list);
                  if(internetIsOn.isOnline())
                  {
                          try {
                                  String DATA = URLEncoder.encode("userName", "UTF-8") + "=" + URLEncoder.encode(sharedPreferenceData.getCurrentUserName(), "UTF-8");
                                  //importantData.getAllShoppingCost(getResources().getString(R.string.shoppingCost), DATA,infoInterfaces);
                                  TestBackground testBackground = new TestBackground(this);
                                  testBackground.setOnResultListener(infoInterfaces);
                                  testBackground.execute(getResources().getString(R.string.shoppingCost),DATA);
                          } catch (UnsupportedEncodingException e) {
                                  e.printStackTrace();
                          }
  
                  }
          }
  
          //process shopping only_show_cost json data
          private void processJsonData(String result)
          {
                  List&lt;CostModel&gt; costList = new ArrayList&lt;&gt;();
                  int count=0;
                  String name,taka,date,id;
                  try {
                          JSONObject jsonObject = new JSONObject(result);
                          JSONArray jsonArray = jsonObject.optJSONArray("costList");
  
                          while (count&lt;jsonArray.length())
                          {
                                  JSONObject jObject = jsonArray.getJSONObject(count);
                                  id = jObject.getString("id");
                                  name = jObject.getString("name");
                                  taka = jObject.getString("taka");
                                  date = jObject.getString("date");
  
                                  costList.add(new CostModel(id,name,taka,date,""));
                                  count++;
                          }
  
                          if(costList.isEmpty())
                                  listView.setEmptyView(emptyView);
                          else
                                  emptyView.setVisibility(View.INVISIBLE);
  
                          CostAdapter adapter = new CostAdapter(this, costList);
                          listView.setAdapter(adapter);
                  } catch (JSONException e) {
                          e.printStackTrace();
                  }
          }
  
          //get all shopping list
          OnAsyncTask infoInterfaces = new OnAsyncTask() {
                  @Override
                  public void onSuccess(final String result) {
                          runOnUiThread(new Runnable() {
                                  @Override
                                  public void run() {
                                          if(result!=null)
                                                  processJsonData(result);
                                  }
                          });
                  }
          };
  }
  </code></pre>
  
  <p>AsyncTask</p>
  
  <pre><code>public class TestBackground extends AsyncTask&lt;String,Void,String&gt;
  {
       private Context context;
       private OnAsyncTask onAsyncTaskInterface;
  
       public TestBackground(Context context)
       {
            this.context = context;
       }
  
  
       //success result
       public void setOnResultListener(OnAsyncTask onAsyncResult) {
            if (onAsyncResult != null) {
                 this.onAsyncTaskInterface = onAsyncResult;
            }
       }
  
       @Override
       public String doInBackground(String... params) {
  
            String result = "";
            String fileName = params[0];
            String postData = params[1];
  
            try {
                 URL url = new URL(fileName);
                 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                 httpURLConnection.setRequestMethod("POST");
                 OutputStream outputStream = httpURLConnection.getOutputStream();
                 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
                 BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
  
                 bufferedWriter.write(postData);
                 bufferedWriter.flush();
                 bufferedWriter.close();
                 outputStream.close();
                 InputStream inputStream = httpURLConnection.getInputStream();
                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  
                 String line;
  
                 while ((line= bufferedReader.readLine())!=null)
                      result = line;
  
                 bufferedReader.close();
                 inputStream.close();
                 httpURLConnection.disconnect();
                 onAsyncTaskInterface.onSuccess(result);
  
            } catch (MalformedURLException e) {
                 e.printStackTrace();
            } catch (IOException e) {
                 e.printStackTrace();
            }catch (Exception e)
            {
                 e.printStackTrace();
            }
            return result.toString();
       }
  
  
        public interface OnAsyncTask {
                  void onSuccess(String message);
          }
  }
  </code></pre>
  
  <p>php code</p>
  
  <pre><code>&lt;?php
  
      require"connection.php";
      require"getSession.php";
  
      $userName = $_POST["userName"];
      $session = new Sessions();
      $month = $session-&gt;session($userName);
  
      $sql = "SELECT GroupID FROM member_info WHERE UserName LIKE '$userName';";
      $result = mysqli_query($conn,$sql);
  
      if($result-&gt;num_rows&gt;0)
      {
          while($row=mysqli_fetch_array($result))
          {
              if($row["GroupID"]!="Null")
                  getCostList($row["GroupID"],$month);
          }
      }
  
      function getCostList($value,$month)
      {
          require"connection.php";
          $sql = "SELECT * FROM cost WHERE groupName LIKE '$value' and cMonth LIKE '$month';";
          $result = mysqli_query($conn,$sql);
  
          $response = array();
  
          if($result-&gt;num_rows&gt;0)
          {
              while($row=mysqli_fetch_array($result))
                  array_push($response,array("id"=&gt;$row[0],"name"=&gt;$row[1],"taka"=&gt;$row[4],"date"=&gt;$row[5]));
  
              echo json_encode(array("costList"=&gt;$response));
          }
      }
  
      $conn-&gt;close();
  ?&gt;
  </code></pre>