HttpURLConnection无法openConnection

时间:2017-02-14 16:16:08

标签: android httpurlconnection

@Override
protected Void doInBackground(String... params) {
    String type = params[0];
    String url= "http://10.0.2.2/login.php" ;
    if(type.equals("Singin"))  {
        try {
            URL Singin_url = new URL(url);
            //Cannot not resolve method 'openConnection()'
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    return null;
}

为什么不让它让OpenConnection? 我不能前进,因为它说'连接'没有初始化

1 个答案:

答案 0 :(得分:1)

您需要在openConnection()对象URL上调用Singin_url方法。您在String对象上调用它。

@Override
protected Void doInBackground(String... params)
{
    String type = params[0];
    String url= "http://10.0.2.2/login.php" ;
    if(type.equals("Singin"))
    {
        try {
            URL Singin_url = new URL(url);
            HttpURLConnection connection = (HttpURLConnection)Singin_url.openConnection();    
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }    
    }
    return null;
}

此外,您的变量名称第一个字符应为小写,例如signInUrl