2017-02-14 160 views
-2
@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? ,我不能前進,因爲它說'連接'未初始化HttpURLConnection無法打開連接

+0

我在擴展AsyncTask的類中使用它 – Basit

回答

1

您需要調用URL對象Singin_urlopenConnection()方法。您在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