Android通用异步任务
我当前有多个活动需要为http post执行asynctask,我希望将asynctask作为另一个类文件,以便不同的活动可以调用asynctask来执行http post请求,而onPostExecute则在调用asynctask的活动中调用方法httpResult(result)。我已尝试传入该活动,但无法在onPostExecute中调用该方法。我该怎么做Android通用异步任务,android,android-asynctask,Android,Android Asynctask,我当前有多个活动需要为http post执行asynctask,我希望将asynctask作为另一个类文件,以便不同的活动可以调用asynctask来执行http post请求,而onPostExecute则在调用asynctask的活动中调用方法httpResult(result)。我已尝试传入该活动,但无法在onPostExecute中调用该方法。我该怎么做 public class MyActivity extends Activity { public void onCreate
public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_MyActivity);
//logic here...
AsyncHttpPost asyncHttpPost = new AsyncHttpPost("someContent", this, dialog);
JSONObject data = new JSONObject();
data.put("key", "value");
try {
asyncHttpPost.execute(data);
}
catch (Exception ex){
ex.printStackTrace();
}
}
public static void httpResult(String result) {
//this method has to get called by asynctask to make changes to the UI
}
}
AsyncHttpPost.java
public class AsyncHttpPost extends AsyncTask<JSONObject, String, String> {
String recvdjson;
String mData="";
private ProgressDialog dialog;
private Activity activity;
public AsyncHttpPost(String data, Activity activity, ProgressDialog dialog) {
mData = data;
this.activity = activity;
this.dialog = dialog;
}
protected void onPreExecute()
{
dialog.show();
}
@Override
protected String doInBackground(JSONObject... params) {
//logic to do http request
return "someStringResult";
}
protected void onPostExecute(String result) {
dialog.dismiss();
activity.httpResult(result); //This line gives me error : The method httpResult(String) is undefined for the type Activity
}
}
公共类AsyncHttpPost扩展了AsyncTask{
字符串recvdjson;
字符串mData=“”;
私人对话;
私人活动;
公共异步HttpPost(字符串数据、活动、进度对话框){
mData=数据;
这个。活动=活动;
this.dialog=dialog;
}
受保护的void onPreExecute()
{
dialog.show();
}
@凌驾
受保护字符串doInBackground(JSONObject…参数){
//执行http请求的逻辑
返回“someStringResult”;
}
受保护的void onPostExecute(字符串结果){
dialog.dismise();
activity.httpResult(result);//这一行给出了错误:类型activity的方法httpResult(String)未定义
}
}
在单独的文件中创建一个名为HttpResponseImpl的接口,并添加所需的方法httpResult
interface HttpResponseImpl{
public void httpResult(String result);
}
现在通过Activity
class实现这个接口
public class MyActivity extends Activity implements HttpResponseImpl{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_MyActivity);
//logic here...
AsyncHttpPost asyncHttpPost = new AsyncHttpPost("someContent", this, dialog);
JSONObject data = new JSONObject();
data.put("key", "value");
try {
asyncHttpPost.execute(data);
}
catch (Exception ex){
ex.printStackTrace();
}
}
public void httpResult(String result){
//this method has to get called by asynctask to make changes to the UI
}
}
您的AsyncHttpPost类将是
public class AsyncHttpPost extends AsyncTask<JSONObject, String, String> {
String recvdjson;
String mData="";
private ProgressDialog dialog;
private HttpResponseImpl httpResponseImpl;
public AsyncHttpPost(String data, HttpResponseImpl httpResponseImpl, ProgressDialog dialog) {
mData = data;
this.httpResponseImpl = httpResponseImpl;
this.dialog = dialog;
}
protected void onPreExecute()
{
dialog.show();
}
@Override
protected String doInBackground(JSONObject... params) {
//logic to do http request
return "someStringResult";
}
protected void onPostExecute(String result) {
dialog.dismiss();
httpResponseImpl.httpResult(result);
}
}
公共类AsyncHttpPost扩展了AsyncTask{
字符串recvdjson;
字符串mData=“”;
私人对话;
私有HttpResponseImpl HttpResponseImpl;
公共异步HttpPost(字符串数据,HttpResponseImpl HttpResponseImpl,ProgressDialog){
mData=数据;
this.httpResponseImpl=httpResponseImpl;
this.dialog=dialog;
}
受保护的void onPreExecute()
{
dialog.show();
}
@凌驾
受保护字符串doInBackground(JSONObject…参数){
//执行http请求的逻辑
返回“someStringResult”;
}
受保护的void onPostExecute(字符串结果){
dialog.dismise();
httpResponseImpl.httpResult(结果);
}
}
使用所有您想要执行HttpRequest的
活动
类实现此HttpResponseImpl
接口。通用异步任务示例
就像
new RetrieveFeedTask(new OnTaskFinished()
{
@Override
public void onFeedRetrieved(String feeds)
{
//do whatever you want to do with the feeds
}
}).execute("http://enterurlhere.com");
检索FeedTask.class
class RetrieveFeedTask extends AsyncTask<String, Void, String>
{
String HTML_response= "";
OnTaskFinished onOurTaskFinished;
public RetrieveFeedTask(OnTaskFinished onTaskFinished)
{
onOurTaskFinished = onTaskFinished;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected String doInBackground(String... urls)
{
try
{
URL url = new URL(urls[0]); // enter your url here which to download
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = br.readLine()) != null)
{
// System.out.println(inputLine);
HTML_response += inputLine;
}
br.close();
System.out.println("Done");
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return HTML_response;
}
@Override
protected void onPostExecute(String feed)
{
onOurTaskFinished.onFeedRetrieved(feed);
}
}
你能发布你的代码并解释问题吗..你应该发布你的代码以使问题更容易解决。用示例代码编辑我的问题从函数
httpResult
中删除静态标识符,因此将public static void httpResult(字符串结果)
更改为public void httpResult(字符串结果)
Sherif:在我从httpResult函数中删除static后出现了相同的错误这将适用于单个活动,但我有一些活动也将使用此asynctask,我不可能列出所有,我说的对吗?亲爱的,你的方法是错误的,你正在传递一个活动
类,它没有自己的方法httpResult(字符串结果)
。根据你的要求,你应该尝试回调机制或观察者侦听器模式。Anieh:如果你能提供代码示例供我尝试,我将不胜感激?
public interface OnTaskFinished
{
public void onFeedRetrieved(String feeds);
}