I have a REST api deployed on a server which contains a ssl certificate. When I hit the api from my android app I was getting trust anchor certificate error so I added the code to my hander to trustall hosts but now I'm getting IOException
on the url. Please suggest what should I do?
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();
}
}
In doInBackground
method of my activity I call
HttpHandler sh = new HttpHandler();
String url = myUrl;
What should I do for this? Please help me.
LogTrace:
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)
I have changed the actual url to myUrl in logs due to some reasons plz ignore that. AFter debugging i got to know that i am getting error at InputStream in=new BufferedInputStream(conn.getInputStream());
please help in solving it