有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android Studio中ssl认证url上引发java IOException

我在包含ssl证书的服务器上部署了一个RESTAPI。当我在安卓应用程序中点击api时,我得到了信任锚证书错误,因此我将代码添加到我的hander中,以信任所有主机,但现在我在url上得到了IOException。请建议我该怎么做

HttpHandler。java

package abc.com.abcpos.Handlers;

import 安卓.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class HttpHandler {

    private static final String TAG = HttpHandler.class.getSimpleName();

    public HttpHandler(){

    }

    public String makeServiceCall(String reqUrl){
        String response=null;
        try{
            TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
            };

            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };

            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
            URL url=new URL(reqUrl);
            HttpURLConnection conn=(HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");

            //read the response
            InputStream in=new BufferedInputStream(conn.getInputStream());
            response=convertStreamToString(in);
        }catch (MalformedURLException e){
            Log.e(TAG, "MalformedURLException: "+e.getMessage() );
        }catch (ProtocolException e){
            Log.e(TAG, "ProtocolException: "+e.getMessage() );
        }catch (IOException e){
            Log.e(TAG, "IOException: "+e.getMessage());
        }catch (Exception e){
            Log.e(TAG, "Exception: "+e.getMessage() );
        }
        Log.d(TAG, "makeServiceCall: "+response);
        return response;
    }


    private String convertStreamToString(InputStream is) {

        BufferedReader reader=new BufferedReader(new InputStreamReader(is));
        StringBuilder sb=new StringBuilder();
        String line;
        try {
            while ((line=reader.readLine())!=null){
                sb.append(line).append('\n');
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                is.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

}

在我的活动的doInBackground方法中,我调用

HttpHandler sh = new HttpHandler();
String url = myUrl;

我该怎么办?请帮帮我

日志跟踪:

08-24 13:13:43.774 10431-10436/abc.com.abcpos I/art: Do partial code cache collection, code=62KB, data=61KB
08-24 13:13:43.775 10431-10436/abc.com.abcpos I/art: After code cache collection, code=62KB, data=61KB
    Increasing code cache capacity to 256KB
08-24 13:13:49.668 10431-10485/abc.com.abcpos E/HttpHandler: IOException: myUrl
08-24 13:13:49.668 10431-10485/abc.com.abcpos D/HttpHandler: makeServiceCall: null
08-24 13:13:49.706 10431-10431/abc.com.abcpos D/AndroidRuntime: Shutting down VM
08-24 13:13:49.707 10431-10431/abc.com.abcpos E/AndroidRuntime: FATAL EXCEPTION: main
    Process: abc.com.abcpos, PID: 10431
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String abc.com.abcpos.Models.EmpPersonalDetails.getFirstName()' on a null object reference
        at abc.com.abcpos.forms.PersonalDetailsFormActivity$GetContacts.onPostExecute(PersonalDetailsFormActivity.java:520)
        at abc.com.abcpos.forms.PersonalDetailsFormActivity$GetContacts.onPostExecute(PersonalDetailsFormActivity.java:375)
        at 安卓.os.AsyncTask.finish(AsyncTask.java:660)
        at 安卓.os.AsyncTask.-wrap1(AsyncTask.java)
        at 安卓.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
        at 安卓.os.Handler.dispatchMessage(Handler.java:102)
        at 安卓.os.Looper.loop(Looper.java:165)
        at 安卓.app.ActivityThread.main(ActivityThread.java:6365)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:773)

由于某些原因,我已将日志中的实际url更改为myUrl,plz忽略了这一点。调试后,我知道我在InputStream in=new BufferedInputStream(conn.getInputStream());处遇到错误,请帮助解决它


共 (1) 个答案

  1. # 1 楼答案

    嘿,你应该检查一下你使用的电话类型。你确定它的帖子不会被收到吗