【问题标题】:Throwing an Exception from OnClickListener从 OnClickListener 引发异常
【发布时间】:2018-09-04 10:08:04
【问题描述】:

我是 java 的新手,我在这里尝试做的是每次按下按钮时发送一个 Post 请求。这是我的代码

   public class Remotetask extends AppCompatActivity {

    TextView tv;
    Button btn;

    public void PostRequest(String url_from_text) throws IOException {
        String url = url_from_text;
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        int responseCode = con.getResponseCode();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        //print in String
        tv.setText(response.toString());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eve_aioremote);
        btn = (Button) findViewById(R.id.Button1);
        tv = (TextView) findViewById(R.id.textView);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) throws IOException{
                PostRequest("api endpoint");
            }
        });
    }
}

这里有这行

public void onClick(View v) 抛出 IOException

在这里给我这个错误:

错误:onClick(View) in 无法在 OnClickListener 中实现 onClick(View) 被覆盖的方法不会抛出 IOException

我发现另一个人问了一个类似问题的帖子,但答案真的没有帮助我,因为我对此有点陌生。谢谢你。

【问题讨论】:

  • 抛出runtimeexeption而不是检查异常或直接在onCick方法中处理异常
  • 我建议你使用 volley 或改造库而不是手动代码

标签: java android


【解决方案1】:

从您的 onclick 中删除 IOException 抛出。像这样

@Override
public void onClick(View v) {
  try {
    PostRequest("api endpoint");
   } catch (IOException e){
        e.printStackTrace();
    }
}

onClickListener 的原始方法没有抛出任何异常,这就是它给出错误的原因。

【讨论】:

  • 这是我尝试的第一件事,但如果我这样做了,我会在 "PostRequest("api endpoint"); ”。非常感谢您的快速回复。
  • 好的,知道了!感谢您的快速解决方案!
【解决方案2】:

抛出 IOException 请不要在 Onclick 中抛出这个,因为您已经在 PostRequest 方法中抛出了异常

试试这个:

 btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v){
        PostRequest("api endpoint");
    }
});

【讨论】:

    【解决方案3】:

    实现 try-catch 而不是抛出 IOException ,

     public class Remotetask extends AppCompatActivity {
    
     TextView tv;
     Button btn;
    
     public void PostRequest(String url_from_text) {
     try{
    String url = url_from_text;
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    int responseCode = con.getResponseCode();
    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    //print in String
    tv.setText(response.toString());
    }catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_eve_aioremote);
    btn = (Button) findViewById(R.id.Button1);
    tv = (TextView) findViewById(R.id.textView);
    
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            PostRequest("api endpoint");
        }
    });
    

    }

    【讨论】:

      【解决方案4】:

      这样处理错误

      btn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          try {
              PostRequest("api endpoint");
          } catch (Exception e)‏ {
              //error handling code
          }
      
      }
      });
      

      【讨论】:

      • 是的!知道了!谢谢!
      【解决方案5】:

      尝试此代码片段,请从OnClick 中删除 throw IOException

        public class Remotetask extends AppCompatActivity {
      
          TextView tv;
          Button btn;
      
          public void PostRequest(String url_from_text) throws IOException {
              String url = url_from_text;
              URL obj = new URL(url);
              HttpURLConnection con = (HttpURLConnection) obj.openConnection();
              con.setRequestMethod("GET");
              con.setRequestProperty("User-Agent", "Mozilla/5.0");
              int responseCode = con.getResponseCode();
              BufferedReader in = new BufferedReader(
                      new InputStreamReader(con.getInputStream()));
              String inputLine;
              StringBuffer response = new StringBuffer();
              while ((inputLine = in.readLine()) != null) {
                  response.append(inputLine);
              }
              in.close();
              //print in String
              tv.setText(response.toString());
          }
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_eve_aioremote);
              btn = (Button) findViewById(R.id.Button1);
              tv = (TextView) findViewById(R.id.textView);
      
              btn.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      try{
                      PostRequest("api endpoint");
                      } catch (Exception e)
                        {
                         e.getStackTrace();
                        }
                  }
              });
          }
      }
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-25
        • 1970-01-01
        • 2014-10-07
        • 1970-01-01
        • 2012-11-11
        • 2019-09-06
        • 1970-01-01
        相关资源
        最近更新 更多