如何使用if语句检查是否发生连接错误

时间:2022-12-27 18:15:29

So I have a button that sends a message to a server when i press it. However, I want to make sure that if there is a ConnectionException the button wont click and will return a Toast.

所以我有一个按钮,当我按下它时,它会向服务器发送一条消息。但是,我想确保如果有一个ConnectionException按钮不会单击并返回Toast。

button.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         if (v.getId() == R.id.button) {
              //Send Message to Server
         } else 
             Toast.makeText(getApplicationContext(), "Server hasn't connected", Toast.LENGTH_LONG).show();
     }
}

I was just wondering how I might implement this?

我只是想知道如何实现这个?

4 个解决方案

#1


0  

try this first

先试试这个

button.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         try{
             if (v.getId() == R.id.button) {
                  //Send Message to Server
             }else {
                  throw new serverExceptionerror ("Server hasn't connected"); 
             } 
         }
         catch (serverExceptionerror ex) { 
             Toast.makeText(ex.getMessage(),Toast.LENGTH_LONG).show();
         }
     }
}

#2


2  

Button clicks happening on the UI thread, the main thread, while server communication is happening on IO thread, non-main thread. You need to have a model that will tell you if an error happened and then display your error message.

按钮点击发生在UI线程(主线程)上,而服务器通信发生在IO线程,非主线程上。您需要有一个模型,它会告诉您是否发生了错误,然后显示您的错误消息。

One possible solution is to use an AsyncTask

一种可能的解决方案是使用AsyncTask

#3


0  

In this case , you should use a try-catch statement.

在这种情况下,您应该使用try-catch语句。

try
{
     //statements that may cause an exception
}
catch (exception(type) e(object))‏
{
     //error handling code
}

#4


0  

wrap you code with a try and catch statment. eg.

用try和catch语句包装你的代码。例如。

button.setOnClickListener(new View.OnClickListener() {
    @Override
    try
    {
        public void onClick(View v) {
            if (v.getId() == R.id.button) {
                 //Send Message to Server
            } else
                 Toast.makeText(getApplicationContext(), "Server hasn't connected", Toast.LENGTH_LONG).show();
            }
        }
    }
    catch(exception (type) e)
    {
        //your custom message
    }
}

#1


0  

try this first

先试试这个

button.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         try{
             if (v.getId() == R.id.button) {
                  //Send Message to Server
             }else {
                  throw new serverExceptionerror ("Server hasn't connected"); 
             } 
         }
         catch (serverExceptionerror ex) { 
             Toast.makeText(ex.getMessage(),Toast.LENGTH_LONG).show();
         }
     }
}

#2


2  

Button clicks happening on the UI thread, the main thread, while server communication is happening on IO thread, non-main thread. You need to have a model that will tell you if an error happened and then display your error message.

按钮点击发生在UI线程(主线程)上,而服务器通信发生在IO线程,非主线程上。您需要有一个模型,它会告诉您是否发生了错误,然后显示您的错误消息。

One possible solution is to use an AsyncTask

一种可能的解决方案是使用AsyncTask

#3


0  

In this case , you should use a try-catch statement.

在这种情况下,您应该使用try-catch语句。

try
{
     //statements that may cause an exception
}
catch (exception(type) e(object))‏
{
     //error handling code
}

#4


0  

wrap you code with a try and catch statment. eg.

用try和catch语句包装你的代码。例如。

button.setOnClickListener(new View.OnClickListener() {
    @Override
    try
    {
        public void onClick(View v) {
            if (v.getId() == R.id.button) {
                 //Send Message to Server
            } else
                 Toast.makeText(getApplicationContext(), "Server hasn't connected", Toast.LENGTH_LONG).show();
            }
        }
    }
    catch(exception (type) e)
    {
        //your custom message
    }
}