为什么我在url.openConnection()下得到红色下划线

时间:2017-04-12 08:26:11

标签: java android-studio

我想知道是什么让我url.openConnection收到错误。  错误是:

  

未处理的异常

以下是代码:

switch (v.getId()){
    case R.id.loginbtn:
        String link;
        String username = uname.getText().toString();
        String password = upass.getText().toString();
        HttpURLConnection urlConnection = null;

        try {
            String u = "http://127.0.0.1/android/login.php";
            URL url = new URL(u);
            String urlParams = "username="+username+"&password"+password;

            urlConnection = (HttpURLConnection) url.openConnection();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }

1 个答案:

答案 0 :(得分:0)

你需要捕获IOException

... finally {
 try {
 urlConnection.disconnect();
} catch (IOException ioex) {   }