连接超时后,无法在用户界面上进行烘烤

时间:2015-12-11 17:06:18

标签: java android

我正在尝试为我的应用程序设置超时连接。现在当超时发生时我成功捕获异常,但由于某种原因,Toast失败了。 我知道这个线程正在后台工作,但我想在当前类中实现toast,因为许多其他类实现它。 我得到的错误是:

  

无法在未调用的线程内创建处理程序   Looper.prepare()

这是我的代码:

public class DBcontroller extends AsyncTask <List<NameValuePair>, Void, String>{
 private static String url = "";
        private  Context context = null ;
        public AsyncResponse delegate= null;

        private PropertyReader propertyReader;
        private Properties properties;

        public DBcontroller(Context mycontext,AsyncResponse myresponse) {
            context = mycontext;
            delegate = myresponse;
            propertyReader = new PropertyReader(context);
            properties = propertyReader.getMyProperties("config.properties");
            url = properties.getProperty("url");
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            delegate.preProcess();
        }

        @Override
        protected String doInBackground(List<NameValuePair>... params) {
            return postDataToServer(params[0]);
        }
        @Override
        protected void onPostExecute(String resStr) {
            super.onPostExecute(resStr);
            delegate.handleResponse(resStr);
        }
        private String postDataToServer(List<NameValuePair> list) {
            //check
            for (int i=0;i<list.size();i++)
                Log.d("responseString", list.get(i).toString());
            //check

            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
            HttpConnectionParams.setSoTimeout(httpParameters, 10000);
            HttpClient client = new DefaultHttpClient(httpParameters);
            HttpPost post = new HttpPost(url);

            String responseString = null;
            try {
                post.setEntity(new UrlEncodedFormEntity(list));
                HttpResponse response = client.execute(post);
                HttpEntity entity = response.getEntity();
                responseString = EntityUtils.toString(entity);
                Log.d("responseString1", responseString);

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (ConnectTimeoutException e) {
                //Here Connection TimeOut excepion
                Toast.makeText(this.context, "Your connection timedout", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(responseString!= null)
                 Log.d("responseString2", responseString);
            return responseString;
        }
    }

3 个答案:

答案 0 :(得分:3)

正确的方法是在Toast内显示onPostExecute(),您将使用类似isConnectTimeoutException的布尔变量

...   
...
    } catch (ConnectTimeoutException e) {
      isConnectTimeoutException = true;      
    }
...
...

然后它已经在那个点上的UI线程。:

    @Override
    protected void onPostExecute(String resStr) {
        super.onPostExecute(resStr);
        delegate.handleResponse(resStr);
        if(isConnectTimeoutException){ 
           Toast.makeText(this.context, "Your connection timedout",Toast.LENGTH_LONG).show();
      }
    }

答案 1 :(得分:0)

在runOnUiThread中添加Toast,可以安全地从工作线程创建或更新UI

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(this.context, "Your connection timedout", Toast.LENGTH_LONG).show();
                        }
                    });

答案 2 :(得分:0)

您可以使用:

Handler handler =  new Handler(context.getMainLooper());
    handler.post( new Runnable(){
        public void run(){
            Toast.makeText(context, "Your Message",Toast.LENGTH_LONG).show(); 
        }
    });

您必须在toast方法中显示doInBackground