package com.jiuqi.njt.register;
import java.lang.reflect.Method;
import com.jiuqi.mobile.nigo.comeclose.manager.base.ILoginManager;
import com.jiuqi.mobile.nigo.comeclose.ws.client.ClientContext;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.RepairUtils;
import com.jiuqi.njt.util.UIUtil;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
/**
* 访问服务器抽象异步类对象
*
* @author Joe
*
*/
public class TestTask extends AsyncTask<Void, Void, String> {
private Context context;
private AllTaskInterface allTaskInterface;
private Class<?> managerClass;
private String methodName;
private Class[] parameterTypes;
private MyApp application;
private Dialog pd;
private Object[] args;
private Object data = null;
private boolean showDialog;
private int dialogType = 5;
/**
* 访问服务器异步类抽象方法
* @param context
* 上下文环境
* @param allTaskInterface
* 返回数据抽象接口对象
* @param managerClass
* 访问的服务器接口类
* @param methodName
* 访问的接口类方法名
* @param parameterTypes
* 访问的接口方法参数类型数组
* @param args
* 访问的接口方法接口参数数组
* @param dialogType
* 显示的对话框类型1 :正在定位...2:正在获取天气...3:正在验证账号... 4:正在提交数据...5:正在获取数据... 6:正在验证手机号码...
*/
public TestTask(Context context, AllTaskInterface allTaskInterface,
Class<?> managerClass, String methodName, Class[] parameterTypes,
Object[] args,boolean showDialog,int dialogType) {
super();
this.context = context;
this.allTaskInterface = allTaskInterface;
this.managerClass = managerClass;
this.methodName = methodName;
this.parameterTypes = parameterTypes;
this.args = args;
this.showDialog = showDialog;
this.dialogType = dialogType;
application = (MyApp) context.getApplicationContext();
}
@Override
protected void onPreExecute() {
if(showDialog){
pd = RepairUtils.myShowDialog(context, dialogType);
}
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
ClientContext context;
String result = "";
try {
context = application.getClientContext();
if (null == context) {
context = ClientContext.getClientContext(Constants.SERVER_URL,Constants.ANONYMOUS, Constants.ANONYMOUS);
application.setClientContext(context);
}
Object manager =context.getManager(managerClass);
Method echoMethod = managerClass.getMethod(methodName,parameterTypes);
data = echoMethod.invoke(manager, args);
if(managerClass==ILoginManager.class){
context = ClientContext.getClientContext(Constants.SERVER_URL, (String)data);
application.setClientContext(context);
}
} catch (Exception e) {
e.printStackTrace();
if(null!= e.getMessage()){
result = e.getMessage();
}else{
result = "服务端异常,请稍后重试";
}
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(showDialog){
RepairUtils.myRemoveDialog(pd);
if(!"".equals(result)){
UIUtil.showMsg(context, result);
}
}
if(null!=allTaskInterface){
allTaskInterface.taskFinishReturn(data);
}
}
}