私はGenerateOtp
という名前AsynkTaskクラスが含まれていると私はCGenerateOtpを実行したいLogin
という名前のクラスを持ってCRegistrationScreen
という名前のクラスがあります。アンドロイドの他のアクティビティで他のクラスのAsynkTaskを呼び出す方法は?
どうすればいいですか?ここで
私CREgistrationクラスAsynkTaskコードは次のとおりです。 -
// request to server for otp generation....................
private class CGenerateOtp extends AsyncTask<String, Void, String> {
@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();
}
}
}
、ここで私のログインクラスのコードです:
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
}
}
変更アクセス修飾子クイックスタート:D – Sanny