2016-02-13 152 views
0

我想連接我的android設備到我的sql數據庫使用我的本地ip地址我的電腦,但沒有任何反應。我有錯誤。httpurlconnection本地ip地址

超時而檢索用於BufferedReader中[在BufferedReader.class所附的javadoc [java.io中的[在E:\安裝\應用\ Eclipse的\ SDK \ Android的SDK-WINDOWS \平臺\ Android的23 \一個droid.jar]]]

從我的logcat

了java.lang.RuntimeException:發生錯誤而執行doInBackground()

引起:java.lang.ClassCastException:libcore.net。 http.HttpURLConnectionImpl不能轉換爲javax.net.ssl.HttpsURLConnection

class BackgroundTask extends AsyncTask< Void, Void, String>{ 

    String json_url = "http://192.168.1.106/sample/sample.php"; 
    HttpURLConnection httpsURLConnection = null; 

    @Override 
    protected String doInBackground(Void... params) { 
     try { 
      URL url = new URL(json_url); 
      httpsURLConnection = (HttpsURLConnection) url.openConnection(); 
      InputStream inputStream = httpsURLConnection.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
      StringBuilder stringBuilder = new StringBuilder(); 

      while((JSON_STRING = bufferedReader.readLine()) != null){ 
       stringBuilder.append(JSON_STRING+"\n"); 
      } 

      bufferedReader.close(); 
      inputStream.close(); 
      httpsURLConnection.disconnect(); 

      return stringBuilder.toString().trim(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 

    @Override 
    protected void onPostExecute(String result) { 
     TextView textView = (TextView) findViewById(R.id.tvDisplay); 
     textView.setText(result); 
    } 
} 

任何建議,將不勝感激。

+0

將錯誤添加到您的帖子。在這篇文章中,你錯過了一個'}'。我希望它存在於你的代碼中。 –

+0

超時檢索BufferedReader [在BufferedReader.class [在E:\ Installer \ Application \ Eclipse \ sdk \ android-sdk-windows \ platforms \ android-23 \ android.jar]中的java.io中附加的javadoc] ] – ariel

+0

java.lang.RuntimeException:執行doInBackground()時發生錯誤 - 來自我的日誌cat – ariel

回答

0

在這一行 -

httpsURLConnection = (HttpsURLConnection) url.openConnection(); 

你應該讓 -

httpsURLConnection = (HttpURLConnection) url.openConnection(); 

有http後面是一個額外秒。你正在把它投向錯誤的班級。

+1

並且重命名:'httpsURLConnection'到'httpURLConnection'不會讓人困惑。 –

+1

ahh ok謝謝hahaha – ariel

+0

我沒有注意到哈哈 – ariel

0

如果您使用'http'與本地服務器交談,請使用HttpURLConnection代替HttpsURLConnection。而'HttpsURLConnection'是從'HttpURLConnection'繼承的,當你錯誤地轉換它時不會給你錯誤,但當你實際上通過http進行連接時它可能會給你錯誤。