从OnClickListener引发异常

时间:2018-09-04 10:08:05

标签: java android

我是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");
            }
        });
    }
}

此行

  

公共无效onClick(View v)引发IOException

在这里给我这个错误:

错误:中的onClick(View)无法在OnClickListener中实现onClick(View) 重写的方法不会引发IOException

我发现另一个人问类似的问题,但答案确实并没有帮助我,因为我对此很陌生。谢谢。

5 个答案:

答案 0 :(得分:1)

从onclick上删除该IOException引发。像这样

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

onClickListener的原始方法不会引发任何异常,这就是为什么它会导致错误。

答案 1 :(得分:1)

引发IOException ,请不要在Onclick中引发此异常,因为您已经在PostRequest方法中引发了异常

尝试一下:

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

答案 2 :(得分:1)

实现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");
    }
});

}

答案 3 :(得分:1)

处理此类错误

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

}
});

答案 4 :(得分:0)

尝试使用此代码段,请从IOException中删除抛出OnClick

  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();
                  }
            }
        });
    }
}

希望这可能对您有帮助