title: I have a function, and in it i am running an thread using Anonymous class, so how to return value to that function
body: <p>The code below will make it more clear:</p>
  <pre><code>public static String TCMResponse(String params, final Context c) {
      final String url = &quot;https://115.248.161.106/ois/API/android/&quot; + params;
      new Thread(new Runnable() {
          @Override
          public void run() {
              String response=&quot;&quot;;
  
              try {
                  Document doc = Jsoup.connect(url).validateTLSCertificates(false).timeout(6000).get();
                  response = doc.text();
  
              }
              catch (IOException e) {
                  Log.d(&quot;Err&quot;,&quot;External OIS not reachable!&quot;);
              }
  
              // I want to return 'response' here, for the TCMResponse()
          }
      }).start();
  }
  </code></pre>
  <p>So as you can see from the code, there is a function, TCMResponse() which takes the parameters of the url which i pass, and it does web scraping, i know all these can be done using volley/ JSONParser easily. But i am just experimenting, how to parse using web scraping.</p>
  <p>So after the page is scraped, i need that function to return the response of the scraped page,</p>
  <p>I've used Callable with executor service, but it again freezes the thread..
  Have a look on what i've done:</p>
  <pre><code>public static String TCMResponse(String params, final Activity act) {
  
      StrictMode.ThreadPolicy policy = new
              StrictMode.ThreadPolicy.Builder()
              .permitAll().build();
      StrictMode.setThreadPolicy(policy);
  
      final String url = &quot;https://115.248.161.106/ois/API/android/&quot; + params;
      response=&quot;&quot;;
  
      class MyBgThread implements Callable&lt;String&gt;
      {
          @Override
          public String call() throws Exception {
              try{
                  Document doc = Jsoup.connect(url).validateTLSCertificates(false).timeout(6000).get();
                  return doc.text();
              }catch (Exception e)
              {
                  Log.d(&quot;Exception&quot;,e.toString());
                  Snackbar.with(act, null)
                          .type(Type.ERROR)
                          .message(&quot;Something got wrong!&quot;)
                          .duration(Duration.LONG)
                          .show();
                  return &quot;{'auth':'false'}&quot;;
              }
          }
      }
  
      Callable&lt;String&gt; worker = new MyBgThread();
      ExecutorService ex = Executors.newSingleThreadExecutor();
      Future&lt;String&gt; future = ex.submit(worker);
      try{
          response = future.get();
      }catch(Exception e)
      {
          Log.d(&quot;Thread Ex&quot;,e+&quot;&quot;);
      }
      ex.shutdown();
      return response;
  }
  </code></pre>