AsyncTask的机器人 - 设计模式和返回值 [英] AsyncTask Android - Design Pattern and Return Values

查看:108
本文介绍了AsyncTask的机器人 - 设计模式和返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个验证的登录凭据外部Web服务器的应用程序 - 让我创建一​​个登录界面的基本问题,当在后台提交将发送一个HTTP请求到服务器并不会造成用户界面挂起 - 同时给用户提供一个ProgressDialog

I'm writing an application that validates login credentials on an external webserver - so I have the basic issue of creating a login screen that when submitted will send an HTTP request to a server in the background and not cause the UI to hang - whilst providing a ProgressDialog to the user.

我的问题在于,我想写一个扩展AsyncTask的一个通用的HTTP请求类的,所以当我打电话 .execute()然后,我将传递字符串参数,这可能有点类似于'后',而当 doInBackground 被称为这将看到邮报的字符串,然后转发这些参数在上我的课的各个呼叫。伪code会是这样

My problem lies in, I want to write a generic HTTP Request class that extends AsyncTask, so when I call .execute() I will then pass String parameters which may contain something like 'post', and when doInBackground is called this will see the 'post' string and then forward those parameters onto the respective call in my class. Pseudo code would be something like

public class HTTPOperations extends AsyncTask<String, Void, String>
{
doInBackground(String... string1,additionalParams)
{
  if string1.equals "post"
      response = httpPost(additionalParams)
       return response;
}

httpPost(params)
{
// do http post request
}
}

这是所有我能想到的,除了创建类的每个HTTP发布/ GET等要求我愿和扩展AsyncTask的...

This is all I could think of, other than creating a class for every HTTP Post/GET etc request I wish to make and extending ASyncTask...

这使我对我的下一个问题,如果HTTP POST是成功的,它返回一个认证令牌,我怎么访问此令牌?

Which leads me to my next problem, if the HTTP POST is successful and it returns an authentication token, how do I access this token?

由于新httpOperations.execute(),不返回从doInBackground的字符串,但类型的值

Because new httpOperations.execute(), does not return the string from doInBackground, but a value of type

抱歉,如果这没有任何意义,我不明白这一点的。请询问拟订如果你需要它。 AsyncTask的设计模式和理念都非常欢迎。

Sorry if this doesn't make sense, I can't figure this out at all. Please ask for elaboration if you need it. AsyncTask design patterns and ideas are hugely welcomed.

推荐答案

如果你设计的是这样的一个可重用的任务,你需要确定一个可重复使用的返回类型。它在你的部分设计决定。问问自己,是我的HTTP操作均与他们被称为以及在其数据处理机制相似?如果是这样,你可以设计一个类来做到这两点。如果没有,你可能需要不同类别为不同的远程操作。

If you are designing a reusable task for something like this, you need to identify a reusable return type. Its a design decision on your part. Ask yourself, "Are my HTTP operations similar in both the mechanisms with which they are called and in which their data is processed?" If so, you can design a single class to do both. If not, you probably need different classes for your different remote operations.

在我的个人使用,我有一个对象,我十分关键值对和共同的返回类型是的 HttpEntity 。这是为HTTP GET和POST的返回类型,这似乎工作确定在我的情况下,因为我会抛出异常在特殊HTTP结果的情况下,像404这种设置的另一个好的方面是,code附加参数以GET或POST非常相似,所以这个逻辑是pretty的施工方便。

In my personal use, I have an object i attach key value pairs to and the common return type is the HttpEntity. This is the return type for both HTTP Get and Post, and this seems to work ok in my scenarios because i throw exceptions in exceptional HTTP result situations, like 404. Another nice aspect of this setup is that the code to attach parameters to a get or post are fairly similar, so this logic is pretty easy to construct.

一个例子是这样的(伪):

An example would be something like this (psuedo):

public interface DownloadCallback {
   void onSuccess(String downloadedString);
   void onFailure(Exception exception);
}

然后在你的code,你去哪里做下载:

Then in your code, where you go to do the download:

DownloadCallback dc = new DownloadCallback(){
   public void onSuccess(String downloadedString){
     Log.d("TEST", "Downloaded the string: "+ downloadedString);
   }
   public void onFailure(Exception e){
     Log.d("TEST", "Download had a serious failure: "+ e.getMessage());
   }
 }

 DownloadAsyncTask dlTask = new DownloadAsyncTask(dc);

然后DownloadAsyncTask的构造函数,存储DownloadCallback,当下载完成或失败,调用对应于事件的下载回调的方法。所以......

Then inside the constructor of DownloadAsyncTask, store the DownloadCallback and, when the download is complete or fails, call the method on the download callback that corresponds to the event. So...

public class DownloadAsyncTask extends AsyncTask <X, Y, Z>(){
  DownloadCallback dc = null;

  DownloadAsyncTask(DownloadCallback dc){
    this.dc = dc;
  }

  ... other stuff ...

  protected void onPostExecute(String string){
    dc.onSuccess(string);
  }
}

我要重申,我想为自己的好,你应该回传HttpEntities。字符串可能看起来是一个不错的主意,现在,但它确实带来了麻烦后,当你想要做的更复杂的逻辑背后的HTTP调用。当然,那取决于你。希望这有助于。

I'm going to reiterate that I think for the good of yourself, you should pass back HttpEntities. String may seem like a good idea now, but it really leads to trouble later when you want to do more sophisticated logic behind your http calls. Of course, thats up to you. Hopefully this helps.

这篇关于AsyncTask的机器人 - 设计模式和返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆