httpurlconnection本地ip地址

时间:2016-02-13 08:42:11

标签: android eclipse httpurlconnection

我正在尝试使用我的计算机的本地IP地址将我的Android设备连接到我的sql数据库,但没有任何反应。我有错误。

  

在为BufferedReader检索附加的javadoc时超时[在BufferedReader.class [在java.io中[在E:\ Installer \ Application \ Eclipse \ sdk \ android-sdk-windows \ platforms \ android-23 \ a 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);
    }
}

任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:0)

在这一行 -

httpsURLConnection = (HttpsURLConnection) url.openConnection();  

你应该成功 -

httpsURLConnection = (HttpURLConnection) url.openConnection(); 

在Http之后还有一个额外的s。你把它扔到错误的班级。

答案 1 :(得分:0)

如果您正在使用&#39; http&#39;使用HttpURLConnection代替HttpsURLConnection。与您的本地服务器通信。虽然&#39; HttpsURLConnection&#39;继承自&#39; HttpURLConnection&#39;,当你错误地投射它时它不会给你错误但是当你实际通过http连接时它可能会给你错误。