如何在异步类android上设置TIMEOUT

时间:2013-11-20 05:44:25

标签: android performance

这是我的布局代码;

   private class mainAdapter extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
          String response = "";
          for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
              HttpResponse execute = client.execute(httpGet);
              InputStream content = execute.getEntity().getContent();

              BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
              String s = "";
              while ((s = buffer.readLine()) != null) {
                response += s;
              }

            } catch (Exception e) {
              e.printStackTrace();
            }
          }
          return response;
        }

        @Override
        protected void onPostExecute(String result) {
            if(flagsearch)
            {   
                pd.dismiss();
                if(result.trim().contains("Result not found !"))
                {
                    Toast.makeText(getApplicationContext(), result.trim(), Toast.LENGTH_LONG).show();

                }
                else
                {
                    String limit=""+0;
                    Intent i= new Intent(Main_Search.this,Main_listview.class);
                    i.putExtra("line", result);
                    i.putExtra("limit",0);
                    i.putExtra("Alert", false);
                    i.putExtra("str_Descrption",str_desc);
                    i.putExtra("str_location", str_location);
                    startActivity(i);   
                }
                //Toast.makeText(getApplicationContext(), ""+result, Toast.LENGTH_LONG).show();
            }
            else
                edit_location.setText(result);      
            str_location= edit_location.getText().toString();
        }

        @Override
        protected  void onPreExecute()
        {
            if(flagsearch)
            {
            pd = ProgressDialog.show(Main_Search.this, "","Please wait...");
            }

        }
      }

我在主要的

上调用asyn类
mainAdapter mm= new mainAdapter();
                String    url="http://www.jobdiagnosis.com/servicesearch.php?keyword="+str_desc+
                        "&location="+str_location;
                url= url.replace(" ","%20");
                mm.execute(url);

程序运行正常,但是某些站点从服务器获取数据需要花费很多时间,然后我想设置time_out但我不知道设置Time_out

Plz帮助我使用TIME_OUT并且还告诉我为什么需要花费时间来获取数据?

我真的很抱歉我的英语不好。

2 个答案:

答案 0 :(得分:2)

doInBackground

AsyncTask中使用此代码
int socket_timeout = 3000;
int connection_timeout = 3000;
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, connection_timeout);
HttpConnectionParams.setSoTimeout(httpParams, socket_timeout);
DefaultHttpClient defaultHttpClient = new DefaultHttpClient(httpParams);
HttpGet httpGet = new HttpGet("http://google.com");
defaultHttpClient.execute(httpGet);

答案 1 :(得分:2)

一种有效的方法是在处理程序中调用AsyncTask,并在postDelayed的给定超时后取消它。

在你的活动中你会有

private void readFromServer(String url, int timeout)
{
    // read from server
    final ServerHit serverHit = new ServerHit ();
    serverHit.execute(url);
    // set a timeout
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (serverHit.getStatus() == AsyncTask.Status.RUNNING)
            {
                serverHit.cancel(true);
            }
        }
    }, timeout);
}

ServerHit是AsyncTask的子类