title: How to call AsynkTask of other class in other Activity in android?
body: <p>I have a class named as <strong><code>CRegistrationScreen</code></strong> which contains AsynkTask class named as <strong><code>GenerateOtp</code></strong> and I have a class named as <strong><code>Login</code></strong> where I want execute CGenerateOtp. </p>
  
  <p>How can I do that?</p>
  
  <p>Here is my CREgistration class AsynkTask code:-</p>
  
  <pre><code>// request to server for otp generation....................
  private class CGenerateOtp extends AsyncTask&lt;String, Void, String&gt; {
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          m_ProgressView.setVisibility(View.VISIBLE);// make progress view Visible to user while doing background process
  
      }
  
      @SuppressWarnings("deprecation")
      @Override
      protected String doInBackground(String... params) {
          InputStream inputStream;
          try {
              // 1. create HttpClient
              HttpClient httpclient = new DefaultHttpClient();
              // 2. make POST request to the given URL
              HttpPost httpPost = new HttpPost(s_szRequestOtpUrl);
              String json;// storing json object
              // 3. build jsonObject
              JSONObject jsonObject = new JSONObject();// json object creation
              jsonObject.put("agentCode", s_szResponseMobileNum);// put mobile number
              jsonObject.put("pin", s_szResponseEncryPassword);// put password
              // 4. convert JSONObject to JSON to String
              json = jsonObject.toString();
              if (BuildConfig.klogInfo)// print json request to server
                  Log.d(TAG, "Server Request:-" + json);
              // 5. set json to StringEntity
              StringEntity se = new StringEntity(json);
              // 6. set httpPost Entity
              httpPost.setEntity(se);
              // 7. Set some headers to inform server about the type of the content
              httpPost.setHeader("Content-type", "application/json");
              // 8. Execute POST request to the given URL
              HttpResponse httpResponse = httpclient.execute(httpPost);
              HttpEntity entity = httpResponse.getEntity();
              // 9. receive response as inputStream
              inputStream = entity.getContent();// get content in response
              if (BuildConfig.klogInfo)
                  Log.d(TAG, "Input stream ...." + inputStream.toString());
              Log.d(TAG, "Response...." + httpResponse.toString());
  
              StatusLine statusLine = httpResponse.getStatusLine();// get status code
              if (BuildConfig.klogInfo)
                  Log.d(TAG, "status line :-" + statusLine);
  
              ////Log.d("resp_body", resp_body.toString());
              int statusCode = statusLine.getStatusCode();
              if (statusCode == 200) {// check if code == 200
                  // 10. convert response  to string and save in result varibale
                  s_szResult = CJsonsResponse.convertInputStreamToString(inputStream);
                  if (BuildConfig.klogInfo)
                      Log.d(TAG, "Server Response.." + s_szResult);
              } else
                  s_szResult = "Did not work!";
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          // 11. return s_szResult
          return s_szResult;
      }
  
      @Override
      protected void onPostExecute(final String response) { // Update UI
          super.onPostExecute(response);
          m_ProgressView.setVisibility(View.GONE);// hiding progressview.....
          try {
              s_m_oResponseobject = new JSONObject(response);// get response from server in json
              getResponse();// server based condition
          } catch (JSONException e) {// handling exception occured by server
              e.printStackTrace();
          }
  
      }
  
      private void getResponse() throws JSONException {/// getting response from server .............
          //if response from server is success
          if (s_m_oResponseobject.getString("resultDesc").equalsIgnoreCase("Transaction Successful")) {
  
              m_oOpenDialog.dismiss();// dismiss dialog
              // and got to OTP verification
              getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, new COtpAutoVerificationScreen()).commit();
          }
      }
  
  }
  </code></pre>
  
  <p>and here is my Login class code:</p>
  
  <pre><code>public class Login extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      return super.onCreateView(inflater, container, savedInstanceState);
  
      // here I want to execute CRegistration AsynkTask  
    }
  }
  </code></pre>