Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.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
Android 如何在此代码中添加AsyncTask超时?_Android_Android Asynctask - Fatal编程技术网

Android 如何在此代码中添加AsyncTask超时?

Android 如何在此代码中添加AsyncTask超时?,android,android-asynctask,Android,Android Asynctask,我在mainActivity[Get_IO()]中启动了一个异步任务: 但是由于连接质量不好,任务仍在等待,我想添加一个超时,但是如何添加? 我尝试添加另一个任务来完成超时,但不幸的是,它不起作用 下面是Get_IO()代码: 公共类Get\u IO扩展异步任务{ @凌驾 受保护的void onPreExecute(){ super.onPreExecute(); //Handler mHandler=新的Handler(); //Runnable Runnable=新的Runnable(){

我在mainActivity[Get_IO()]中启动了一个异步任务:

但是由于连接质量不好,任务仍在等待,我想添加一个超时,但是如何添加? 我尝试添加另一个任务来完成超时,但不幸的是,它不起作用

下面是Get_IO()代码:

公共类Get\u IO扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//Handler mHandler=新的Handler();
//Runnable Runnable=新的Runnable(){
//@覆盖
//公开募捐{
//if(mAT.getStatus()==Status.RUNNING | | mAT.getStatus()==Status.PENDING){
//mAT.cancel(true);//取消异步任务或在1分钟后执行所需的操作
//        }
//    }
//};
//mHandler.postDelayed(可运行,60000);
//显示进度对话框
pDialog=新建进度对话框(PortailActivity.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//创建服务处理程序类实例
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url\u状态,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
string_input=jsonObj.getString(TAG_input);
string_output=jsonObj.getString(TAG_output);
//单触点的tmp哈希映射
HashMap iostatus=新的HashMap();
//将每个子节点添加到HashMap key=>value
输入(标记输入、字符串输入);
put(标记输出、字符串输出);
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
Log.e(“ServiceHandler”,“无法从url获取任何数据”);
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//关闭进度对话框
if(pDialog.isShowing())
pDialog.disclose();
TextView lblMobile=(TextView)findViewById(R.id.mobile_标签);
lblMobile.setText(string_input.concat(“-”).concat(string_output));
检查_etat_portail();
}
}

我不太明白这件事;)

如果要在执行
makeServiceCall()
时启动新活动,应在
onPostExecute()
中执行此操作,并检查
makeServiceCall()
是否成功。如果你想缩短网络超时,你应该为你的HTTP请求设置它(因此基本上至少在
makeServiceCall()
中设置,甚至更低,这取决于你的应用程序结构)

   // Rafraichissmeent de l'activity
    refresh = new Runnable() {
        public void run() {
            // Do something
            new Get_IO().execute();
            handler.postDelayed(refresh, 10000);
        }
    };
    handler.post(refresh);
public class Get_IO extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //Handler mHandler = new Handler();
        //Runnable runnable = new Runnable() {
        //    @Override
        //    public void run() {
        //        if (mAT.getStatus() == Status.RUNNING || mAT.getStatus() == Status.PENDING) {
        //            mAT.cancel(true); //Cancel Async task or do the operation you want after 1 minute
        //        }
        //    }
        //};
        //mHandler.postDelayed(runnable, 60000);

        // Showing progress dialog
        pDialog = new ProgressDialog(PortailActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url_status, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                string_input = jsonObj.getString(TAG_INPUT);
                string_output = jsonObj.getString(TAG_OUTPUT);

                // tmp hashmap for single contact
                HashMap<String, String> iostatus = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                iostatus.put(TAG_INPUT, string_input);
                iostatus.put(TAG_OUTPUT, string_output);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();

        TextView lblMobile = (TextView) findViewById(R.id.mobile_label);
        lblMobile.setText(string_input.concat(" - ") .concat(string_output));
        check_etat_portail();
    }
}