Java 从OnClickListener引发异常
我对java有点陌生,我在这里尝试做的是每次按下按钮时发送Post请求。这是我的密码Java 从OnClickListener引发异常,java,android,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; UR
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");
}
});
}
}
这条线在这里
公共void onClick(视图v)引发IOException
这里给出了一个错误:
错误:中的onClick(视图)无法在OnClickListener中实现onClick(视图)
重写的方法不会引发IOException
我在另一篇帖子中发现一个家伙问了一个类似的问题,但答案真的帮不了我,因为我对这个问题有点陌生。多谢各位 从onclick中删除IOException抛出。像这样
@Override
public void onClick(View v) {
try {
PostRequest("api endpoint");
} catch (IOException e){
e.printStackTrace();
}
}
onClickListener的原始方法不会抛出任何异常,这就是它给出错误的原因。从onclick中删除IOException抛出。像这样
@Override
public void onClick(View v) {
try {
PostRequest("api endpoint");
} catch (IOException e){
e.printStackTrace();
}
}
onClickListener的原始方法不会引发任何异常,这就是它给出错误的原因。引发IOException请不要在Onclick中引发此异常,因为您已经在PostRequest方法中引发异常 试试这个:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
PostRequest("api endpoint");
}
});
抛出IOException请不要在Onclick中抛出此异常,因为您已经在PostRequest方法中抛出异常 试试这个:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
PostRequest("api endpoint");
}
});
实现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");
}
});
}实现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");
}
});
}像这样处理错误
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
PostRequest("api endpoint");
} catch (Exception e) {
//error handling code
}
}
});
像这样处理错误
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
PostRequest("api endpoint");
} catch (Exception e) {
//error handling code
}
}
});
尝试此代码段。请从
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();
}
}
});
}
}
希望这可以帮助您尝试此代码段。请从
OnClick
中删除throwIOException
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();
}
}
});
}
}
希望这可以帮助您抛出runtimeexeption而不是选中的异常,或直接在OnClick方法中处理异常我建议您使用volley或改装库而不是手动代码抛出runtimeexeption而不是选中的异常,或直接在OnClick方法中处理异常我建议您使用volley或改装库这是我尝试的第一件事,而不是手动编写代码,但如果我这样做,我会得到“错误:未报告的异常IOException;必须捕获或声明为抛出”行“PostRequest(“api端点”);”。非常感谢你的快速回复。好的,明白了!感谢您的快速解决方案!这是我尝试的第一件事,但如果我这样做,我会得到“错误:未报告的异常IOException;必须捕获或声明为抛出”这一行“PostRequest(“api端点”);”。非常感谢你的快速回复。好的,明白了!感谢您的快速解决方案!是的!知道了!非常感谢。是的!知道了!非常感谢。