Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么';在Android中,InputStream是否可以使用HTTPS?_Java_Android_Api_Http_Inputstream - Fatal编程技术网

Java 为什么';在Android中,InputStream是否可以使用HTTPS?

Java 为什么';在Android中,InputStream是否可以使用HTTPS?,java,android,api,http,inputstream,Java,Android,Api,Http,Inputstream,当我从开放天气api改为黑暗天空api时,我发现连接有问题 我只想从该api获得JSON响应,通过开放的天气图api,一切都很顺利。现在我决定改用dark sky api。我只是像往常一样调整了一切,但它不起作用 可能是https有问题?(dark sky api使用https URL,而open weather map api使用http URL) 不管怎样,我发现了一个例外 inputStream = connection.getInputStream(); 这是我的班级: import

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

我只想从该api获得JSON响应,通过开放的天气图api,一切都很顺利。现在我决定改用dark sky api。我只是像往常一样调整了一切,但它不起作用

可能是https有问题?(dark sky api使用https URL,而open weather map api使用http URL)

不管怎样,我发现了一个例外

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;
}
}

您必须根据应用程序类更改主机名

检查我的代码

    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) {
        }
    }


}

正如您所提到的,http/https协议只是有所不同。对于具有https协议的URL,必须使用HttpsURLConnection API而不是HttpURLConnection。如果您使用https url连接API,将引发异常。有关更多详细信息,请参阅下面的链接


那是什么异常?你能发布日志cat错误吗?没有异常我调试了它,发现它从被调用的行跳转到返回null。如果没有异常,为什么你的问题会说有异常?我真的不明白你到底在做什么。主机名可能是我的api密钥吗?我需要这个SSLHandshake处理程序吗?请在包中添加应用程序类,并在menifest.xml中添加as。给我你的api url,我建议你的主机名是什么