Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Asynctask类是否要重新运行自身?_Java_Android - Fatal编程技术网

Java Asynctask类是否要重新运行自身?

Java Asynctask类是否要重新运行自身?,java,android,Java,Android,你好,我有一个类,它通过asynctask运行我的getQus类 现在的问题是,我试图以这样一种方式设置它,如果 我的返回结果为null,以使用更新的变量再次运行getQus,即增加我的topic&lvl,直到得到有效的结果 这是我的系统流程。 第一类设置主题和lvl->将主题和lvl传递给getQus->getQus通过onPostExecutedelegate返回结果 一级代码 public void GenerateQus(){ //run code to get new qus

你好,我有一个类,它通过asynctask运行我的getQus类 现在的问题是,我试图以这样一种方式设置它,如果 我的返回结果为null,以使用更新的变量再次运行getQus,即增加我的topic&lvl,直到得到有效的结果

这是我的系统流程。 第一类设置主题和lvl->将主题和lvl传递给getQus->getQus通过onPostExecutedelegate返回结果

一级代码

 public void GenerateQus(){
    //run code to get new qus
    x.setLvl(lvl);
    x.setTopic(topic);
    System.out.println("GENERATEQUS:"+ lvl +" , "+topic);
    new getQus(CAT.this).execute(x);
}
getQus类

    protected Question doInBackground(Level... params) {
    int r = params[0].getLvl();
    int z = params[0].getTopic();
    System.out.println("getQlvl:" + r);
    System.out.println("getQtopic:" + z);

    String range = String.valueOf(r);
    String topic = String.valueOf(z);

    // TODO Auto-generated method stub
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("range",range));
    nameValuePairs.add(new BasicNameValuePair("topic",topic));
    int q_id = 0;
    String result=null;
    String q_qus =" ";
    String result2 = " ";

    Question q = new Question();


     InputStream is = null;
        try {
            Globals g = Globals.getInstance();
               String ip = g.getip();
              HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://"+ip+"/fyp/qus.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }
        //convert response to string
        try{

            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();

                result2=sb.toString();
                System.out.println("TEDASD: "+result2);

                if(result2.equals("[]")){
                    //to rerun again
                }

我应该怎么做才能让getQus再次调用自己?提前谢谢

在onPostExecute方法中,您将得到结果。因此,如果结果为空,则启动AYSNTASK。如果我正确理解了您的问题,您只需在doInBackground中重新运行逻辑。那么,你为什么不这样做呢

protected Question doInBackground(Level... params) {
    Question q;
    while((q = doTheStuff(params))==null){
        updateParams(params);
    }
    return q;
}

private Question doTheStuff(Level.. params){
    //your logic here
}

private void updateParams(Level.. params){
    //update params here
}
如果只能在主线程中更新x,则可以在onPostExecute中重新执行任务:

public class SomeActivity extends Activity {

    private X x = new X();

    @Override protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new AsyncTask<X, String, Long>() {

            @Override protected Long doInBackground(final X... params) {
                //  do something here
                return null;
            }

            @Override protected void onPostExecute(final Long result) {
                if (result == null) {
                    //  update args and restart task
                    x.setFoo("a");
                    x.setBar("b");
                    execute(x);
                }

            }
        }.execute(x);
    }

    private class X {
        String foo;
        String bar;

        public void setFoo(final String foo) {
            this.foo = foo;
        }

        public void setBar(final String bar) {
            this.bar = bar;
        }
    }
}

嗨,是的,我怎么再开始呢?谢谢你为什么想重新开始。我的意思是,如果您没有得到结果,那么您的asyncTask将继续运行。您好,因为我运行这个getqus类来检索基于级别和主题的问题详细信息。如果因为没有结果而返回null,我想使用自动更新的变量重新运行该类,直到得到有效的响应。如果返回null,我将设置lvl++,topic++并再次运行该方法。如果失败,将继续运行++直到得到有效结果。我试图让getqus方法将一个适当的问题对象传回给我的主类,而不是空的。