title: Why my setDefaultHostnameVerifier() didn't work for the first time?
body: <p>Here is my code:</p>
  
  <pre><code> private void upload() {
        Task task = new Task();
        task.execute();
   }
  
  class Task extends AsyncTask&lt;String, String, String&gt; {
  @SuppressLint("NewApi")
  @Override
  protected String doInBackground(String... params) {
    try {
      URL mUrl = new URL(HttpUrls.getUrl() + HttpUrls.UPLOAD_IMAGE);
      SSLContext sslContext = Global.getSSLContext(AppConfig.getApplicationContext());
      HttpsURLConnection conn = (HttpsURLConnection) mUrl
          .openConnection();        conn.setDefaultHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
      conn.setSSLSocketFactory(sslContext.getSocketFactory());
      conn.setDoInput(true);
      conn.setDoOutput(true);
      conn.setRequestMethod("POST");
      conn.setRequestProperty(Constants.Field.USER_UUID, mProductUuid);
      conn.setRequestProperty(Constants.Field.IMG_TYPE, IMGType.PDT_IMG.name()); 
      conn.setRequestProperty("Connection", "Keep-Alive");
      conn.setRequestProperty("Charset", "UTF-8");
      conn.setRequestProperty("Content-Type",
              "multipart/form-data;boundary=" + boundary);
      OutputStream output = conn.getOutputStream();
      if (StringUtils.isEmpty2(mImgPath)) {
        output.write(begin(System.currentTimeMillis() + ".jpg"));
      } else {
        output.write(begin(mImgPath));
      }
      if (mUri != null) {   
        Bitmap bitmap = ImageFactory.getBitmapFormUri(GalleryActivity.this, mUri, true);
        output.write(ImageFactory.bitmapToByteAAA(bitmap));
        try {
          bitmap.recycle();
        } catch (Exception e) {
        }finally{
          if(bitmap!=null &amp;&amp; !bitmap.isRecycled()){
            bitmap.recycle();
            bitmap = null;
          }
        }
      } else {
        output.write(ImageFactory.bitmapToByteBBB(mBitmap));
      }
      output.write(end());
      output.flush();
      output.close();
      return IOUtils.toString(conn.getInputStream());
    } catch (Exception e) {
      e.printStackTrace();
      return "";
    }
  }
  </code></pre>
  
  <p>I use the self defined certificate in my server, and I have load the trust.bks in <code>Gloabal.getSSLContext(Application.getApplicationContext())</code> ,and I call the upload() method every time I click the upload button.
  But it is weird that when I click the upload button for the first time, I get the error "hostname is not verified", which means <code>conn.setDefaultHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);</code> in my code didn't work, and when I click the upload button for the second time, it works well, and I upload the file successfully, why? Is it have any delay when I call setDefaultHostnameVerifier()? If it is, how can I make it works for the first time I call upload() method? And can anybody tell me the principle of the method setDefaultHostnameVerifier()?</p>