我正在尝试解决这个问题:我有一个从 AsyncTask 扩展的类,它执行一些 http 方法,如下所示:
public class RESTManager extends AsyncTask<Object, Object, String>
{
public RESTManager(String url, MethodTypes myMethod,ICallbackHandler hndl, String restMethod)
{
initialize();
}
private String doGET()
{
}
private String doPOST()
{
}
@Override
protected String doInBackground(Object... params)
{
}
@Override
protected void onPostExecute(String result)
{
}
}
所以,从活动我这样做:
RESTManager webService = new WSManager(URL, MethodTypes.REGISTER, params, this, WSManager.GET_METHOD);
webService.execute();
然后activity在callbak方法中接收响应(因为activity实现了这个接口):
public void response(String response)
{
// some process
}
这工作正常,但我的问题是我希望答案出现在对某个函数的相同调用中,然后该函数调用 .execute 方法。
// This is a new class
public class MyWebServices()
{
public User getUser(int id)
{
User myUser;
RESTManager webService = new WSManager(URL, MethodTypes.REGISTER, params, this, WSManager.GET_METHOD);
webService.execute();
// BUT I WANT TO RETURN FROM HERE THE CALL FROM THE ACTIVITY....LIKE
return myUser;
}
}
因此,从活动来看,这将是:
MyWebServices myService = new MyWebServices();
User usr = myService.getUser(id);
希望不理解我试图描述的内容,谢谢!