为什么InputStream在Android中不支持HTTPS?

时间:2016-12-30 12:30:13

标签: java android api http inputstream

当我从开放天气api变为黑暗天空api时,我发现连接有问题。

我只是想从那个api获得JSON响应,并且使用开放的天气图api,一切都很顺利。现在我决定改用黑暗的天空api。我只是一如既往地调整了所有内容,但它不起作用。

也许它与https有问题? (黑暗的天空api使用https-URL,其中打开的天气图api使用了http-URL。)

无论如何,我在

中捕获了一个IOException
inputStream = connection.getInputStream();

这是我的班级:

import com.nymvno.hiob.prototyp_v30.Utils.Utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class WeatherHttpClient {

public String getWeatherData(String place) {
    HttpURLConnection connection;
    InputStream inputStream;

    try {
        connection = (HttpURLConnection) (new URL(Utils.BASE_URL + place)).openConnection();
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.connect();

        //Read the response
        StringBuffer stringBuffer = new StringBuffer();
        inputStream = connection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line;

        while ((line = bufferedReader.readLine()) != null) {
            stringBuffer.append(line + "\r\n");
        }
        inputStream.close();
        connection.disconnect();

        return stringBuffer.toString();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
}

2 个答案:

答案 0 :(得分:-1)

您必须从Application类中更改主机名

检查我的代码

    public class MyApp extends Application{
@Override
    public void onCreate(){
        super.onCreate();
handleSSLHandshake();
}

@SuppressLint("TrulyRandom")
    public static void handleSSLHandshake() {
        try {
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                @Override
                public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {

                }

                @Override
                public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {

                }

                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new java.security.cert.X509Certificate[0];
                }


            }};

            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession arg1) {
                    if(hostname.equalsIgnoreCase("your host name")){
                        return true;
                    }else {
                        return false;
                    }

                }
            });
        } catch (Exception ignored) {
        }
    }


}

答案 1 :(得分:-1)

正如您所提到的,http / https协议只有一点不同。对于具有https协议的URLS,您必须使用HttpsURLConnection API而不是HttpURLConnection。如果您使用HttpURLConnection API进行https url异常将被抛出。有关详细信息,请参阅以下链接。

https://developer.android.com/reference/javax/net/ssl/HttpsURLConnection.html