2016-02-13 108 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进行连接时它可能会给你错误。