我在包含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;
我该怎么办?请帮我。
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)
由于某些原因,我已将日志中的实际URL更改为myUrl,请plz忽略该原因。调试后,我知道我在
InputStream in=new BufferedInputStream(conn.getInputStream());
处遇到错误,请帮助解决它 最佳答案
嘿,您应该检查您正在使用的呼叫类型。您确定其POST未获取?