簡體   English   中英

從OnClickListener引發異常

[英]Throwing an Exception from OnClickListener

我是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

在這里給我這個錯誤:

錯誤:無法在OnClickListener中重寫的onClick(View)實現onClick(View)重寫方法不會引發IOException

我發現另一個人問類似的問題,但答案確實並沒有幫助我,因為我對此很陌生。 謝謝。

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

實現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
    }

}
});

試試這個代碼片段,請從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();
                  }
            }
        });
    }
}

希望這對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM