如何管理不同的任务需要由AsyncTask的被称为 [英] How to manage different task which need to be called by AsyncTask

查看:243
本文介绍了如何管理不同的任务需要由AsyncTask的被称为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外部库使用哪个经由因特网的服务器进行通信。每次我需要得到来自互联网,Android的力量我使用AsyncTask的一些信息。到目前为止,没有任何问题。不过,我越来越任务检索(以不同的方式)从互联网上的数据,我不喜欢不同类别的每个单个呼叫的增加。
所以长话短说:我有不同的网络通话,并且而不是创建的AsyncTask类为每个呼叫,我想prefere一个特定的类来管理所有不同的呼叫。这个现象的原因可能吗?而更重要的,什么是这样做的正确方法?


解决方案

我也面临similer问题像你have.But我通过反射技术解决了这个问题。我做了一个方法prevent不同类别的增加调用单发命中。
我做了一个AsyncTask的类,并通过functionName和活动的背景和返回的响应onPostExecute

下面是示例 -

AsyncTaskConnection.java

 公共类AsyncTaskConnection扩展的AsyncTask<字符串,字符串,对象> {
    的JSONObject mainObject;
    上下文mContext;
    串returnFunctionName;    公共AsyncTaskConnection(上下文的背景下){
         mContext =背景;
    }    在preExecute保护无效(){
        // preExecute
    }    @覆盖
    保护对象doInBackground(字符串参数...){
        串apiFunctionName =参数[0]; //获取API FunctionName
        串jsonString =参数[1]; //获取数据
        returnFunctionName = apiFunctionName +反应; //返回函数名
        //一些连接code ...
        //然后调用...
            尝试{
                ht.call空间(namespace,requestEnvelop);
            }赶上(IOException异常前){
                Log.d(mContext.getClass()的getName(),IO异常bufferedIOStream封闭+ EX);
                ex.printStackTrace();
            }
            返回mainObject.toString();
        }赶上(例外五){
            Log.d(异常,e.toString());
            返回不;
        }
    }    //主要的是,我有在这里使用的反光标识....    @覆盖
    保护无效onPostExecute(对象backresult){
        法米;
        尝试{            M = mContext.getClass()getDeclaredMethod(returnFunctionName,为String.class);
            m.invoke(mContext,(字符串)backresult);
        }赶上(IllegalAccessException |抛出:IllegalArgumentException |的InvocationTargetException E){
            e.printStackTrace();
        }赶上(NoSuchMethodException E){
            e.printStackTrace();
        }
    }


  

在调用者类


  //调用这个类,你想,让动态响应
新AsyncTaskConnection(本).execute(getHomepage,jo.toString());//并作出反应机能的保护无效getHomepageResponse(字符串backresult){
            尝试{
//这是你的回应
                mainObject =新的JSONObject(backresult);
            }赶上(JSONException E){
                e.printStackTrace();
            }
        }

和可以有多种途径获得结果是你想要的。

I have a external library to use which communicates with a server via the internet. Everytime I need to get some info from the internet, android forces me to use an asynctask. So far no problem. However, I'm getting more and more tasks to retrieve (in different ways) data from the internet and I'm not liking the increase of different classes for each single call. So long story short: I have different internet-calls, and instead of creating an asynctask-class for each call, I would prefere one single class to manage all different calls. Is that somehow possible? And more importantly, what is the proper way to do so?

解决方案

I was also facing similer problem like you have.But i solved this problem by Reflection Technique. I have made a method to prevent increasing of different classes to call single hit. I have made single asynctask class and passed functionName and context of the activity and returned response by onPostExecute.

Here is sample -

AsyncTaskConnection.java

public class AsyncTaskConnection extends AsyncTask<String, String, Object>{
    JSONObject mainObject;
    Context mContext;
    String returnFunctionName;

    public AsyncTaskConnection (Context context){
         mContext = context;
    }

    protected void onPreExecute() {
        // preExecute
    }

    @Override
    protected Object doInBackground(String... arguments) {
        String apiFunctionName = arguments[0]; // get api FunctionName 
        String jsonString = arguments[1]; // get data
        returnFunctionName = apiFunctionName+"Response"; // return function name
        // some connection code...


        //then call...
            try {
                ht.call(NAMESPACE, requestEnvelop);
            } catch (IOException ex) {
                Log.d(mContext.getClass().getName(), "Io exception bufferedIOStream closed" + ex);
                ex.printStackTrace();
            }
            return mainObject.toString();
        } catch (Exception e) {
            Log.d("Exception", e.toString());
            return "no";
        }
    }

    // main thing is there, i have use the reflaction here....

    @Override
    protected void onPostExecute(Object backresult) {
        Method m;
        try {

            m = mContext.getClass().getDeclaredMethod(returnFunctionName, String.class);
            m.invoke(mContext, (String) backresult);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

in caller class

//call this class where you want and get dynamic response 
new AsyncTaskConnection(this).execute("getHomepage",jo.toString());

// and make response fuction

protected void getHomepageResponse(String backresult) {
            try {
// this is your response
                mainObject = new JSONObject(backresult);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

And there can be many ways to get result that you want.

这篇关于如何管理不同的任务需要由AsyncTask的被称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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