IOException 在 Android Studio 中的 SSL 认证网址上抛出



我在包含SSL证书的服务器上部署了一个REST API。当我从我的 android 应用程序点击 api 时,我收到信任锚证书错误,所以我将代码添加到我的处理程序以信任所有主机,但现在我在 url 上得到了IOException。请建议我该怎么办?

HttpHandler.java

package abc.com.abcpos.Handlers;
import android.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 android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)

由于某些原因,我已经在日志中将实际网址更改为myUrl,请忽略它。AFter 调试我知道我在InputStream in=new BufferedInputStream(conn.getInputStream());遇到错误,请帮助解决它

嘿,您应该检查您正在使用的呼叫类型。你确定它的 POST 不是 GET 吗?

最新更新