I also had a problem with a similer like yours. But I solved this problem with Reflection Technique. I made a method to prevent the increase of different classes in order to cause a single blow. I made a single asynthesis class and passed the functionName and activity context and returned an onPostExecute response.
Here is an example -
AsyncTaskConnection.java
public class AsyncTaskConnection extends AsyncTask<String, String, Object>{
JSONObject mainObject;
Context mContext;
String returnFunctionName;
public AsyncTaskConnection (Context context){
mContext = context;
}
protected void onPreExecute() {
}
@Override
protected Object doInBackground(String... arguments) {
String apiFunctionName = arguments[0];
String jsonString = arguments[1];
returnFunctionName = apiFunctionName+"Response";
try {
ht.call(NAMESPACE, requestEnvelop);
} catch (IOException ex) {
Log.d(mContext.getClass().getName(), "Io exception bufferedIOStream closed" + ex);
ex.printStackTrace();
}
return mainObject.toString();
} catch (Exception e) {
Log.d("Exception", e.toString());
return "no";
}
}
@Override
protected void onPostExecute(Object backresult) {
Method m;
try {
m = mContext.getClass().getDeclaredMethod(returnFunctionName, String.class);
m.invoke(mContext, (String) backresult);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
in caller class
new AsyncTaskConnection(this).execute("getHomepage",jo.toString());
protected void getHomepageResponse(String backresult) {
try {
mainObject = new JSONObject(backresult);
} catch (JSONException e) {
e.printStackTrace();
}
}
And there can be many ways to get the result you want.
source
share