Android的AsyncTask的和物体通过 [英] Android AsyncTask and object passing

查看:127
本文介绍了Android的AsyncTask的和物体通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要周围的登录过程我的code的基本架构一些帮助。我执行异步的Http处理得到的东西的工作ICS

I need some help on the basic architecture of my code around the login process. I'm implementing Async Http processing to get things working for ICS.

这code的目的是为了登录到我的应用程序。

the goal of this code is for logging into my app.


  • 登录形式从UI(Login.java)提交

  • 连接到我们的服务器,并通过HTTP GET XML结果
  • 通过用户名/密码
  • 解析XML结果数组。 (ParseXML.java)

  • 在UI
  • 显示效果反馈。

  • Login form submit from UI (Login.java)
  • connect to our server and pass username / password via http get XML result
  • parse XML results to array. (ParseXML.java)
  • display result feedback in UI.

现在这一切曾在我的程序试图与它执行ASYC HTTP连接我很快就意识到我的问题ICS测试后然而,领着我怀疑我的整个设计...

Now this all worked in my program however after trying to test with ICS which enforces Asyc HTTP connections i quickly realised my problem and led me to doubt my entire design...

目前,它的工作原理的基本途径:

the basic way it currently works:

Login.java:

  class Login {
    ...
    ParseXML myXMLParser = new ParseXML();  
    myXMLParser.doLogin(username, password, Login.this);    

    public doFinished(result) {
      // update UI
    }
    ...
  }

ParseXML.java:

  class ParseXML {
    // class variable to hold login object for async to access
    public Login loginObj;
    ...
    public void doLogin(String _username, String _password, Login _l) {
      loginObj = (Login) _l;
      ...

      // create loginUrl string to run through async
      ...
      new DownloadFilesTask().execute(loginUrl);
    }

    class DownloadFilesTask extends AsyncTask<a, b, c> {
      doInBackground() {
        // do stuff
        // download result of http call
        // parse XML
      }

      onPostExecute(result) {        
        // call the public class variable of login object i have and tell it to update the UI
        // pass back the result array.
        loginObj.doFinished(result);
      }
    }
  }

我最关注,其糟糕的设计是做事这样,我应该只是提出我的Login.java文件中的XML和HTTP连接code所以它包括所有(UI,HTTP,XML解析, ASYC)。

I'm mostly concerned that its bad design to be doing things this way and I should simply move the XML and http connection code within my Login.java file so its all included (UI, HTTP, XML Parsing, Asyc).

我特别很担心回拨Login.doFinished()的从onPostExecute()。这是坏的记忆?我担心这将导致ParseXML对象,以避免垃圾收集,因为它现在要回登录活动一旦用户登录,进而持有ParseXML开放,将继续运行。

Particularly i'm concerned with calling back to Login.doFinished() from onPostExecute(). is this bad for memory? I'm worried that would cause the ParseXML object to avoid garbage collection because it is now going back to Login Activity which will continue running once the user is logged in in turn holding ParseXML open.

我来自一个PHP的背景,所以我一直试图保持 ParseXML 模块,这样我知道到哪里寻找内我所有的XML解析和HTTP处理改变这一点。

I come from a PHP background so I have been trying to keep all my XML parsing and HTTP processing within the ParseXML "module" so that i know where to look for changes to this.

目前 ParseXML 处理所有的HTTP工作,即 getUsers getChannels ADDUSER delUser doLogin 等,但我应该尝试将所有的code办理相关屏幕/活动,使他们在自我包含的XML和HTTP连接(异步)?

At the moment ParseXML handles all http work, ie, getUsers, getChannels, addUser, delUser, doLogin etc. But Should I try moving all the code to handle XML and HTTP connections (async) within the relevant screen/ activity so they are self contained?

我真的AP preciate有这方面的帮助。

I really appreciate any help on this

推荐答案

这正是我们试图避免在面向对象设计,一张大大的类包含的一切(UI的东西,业务逻辑等)。

I should simply move the XML and http connection code within my Login.java file so its all included (UI, HTTP, XML Parsing, Asyc).

This is exactly what we trying to avoid in an OO design, a big damn class contains everything (UI stuff, business logic and etc).

根据您的要求,有良好的面向对象deisgn IMO是:

Based on your requirement, a good OO deisgn IMO is:


  1. 创建界面IBusniessDAO定义所有的方法签名(getUsers,getChannels等)。

  2. 创建一个POJO(AKA。普通Java对象)XMLParser类实现IBusinessDAO,在这个类中,通常写你的方法实施,在此不处理任何异步执行(即不是企业POJO的工作)。在何处以及如何将这些方法都旨在用于(同步或异步)的呼叫者类(即活性)被确定。如果将来说,你想与JsonParser更换XmlParser的,简单的创建JsonParser实现IBusinessDAO并更换XmlParser的。

  3. 的AsyncTask始终保持活动(作为一个内部类),如果需要活动的网络功能,只需初始化您在本活动IbusinessDAO对象,并正确地调用与网络相关的方法AsyncTask.doInBackground()方法,并更新用户界面东西通过这个活动直接在AsyncTask.onPostExecute()方法。管理

检查出样品code我写这个<一个href=\"http://stackoverflow.com/questions/8295003/best-way-to-manage-the-progressdialog-from-asynctask/8317071#8317071\">answer之前,希望这有助于。

Check out the sample code I written in this answer before, hope this helps.

这篇关于Android的AsyncTask的和物体通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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