使用HttpsURLConnection和SSL的NullPointerException

时间:2012-06-22 04:20:07

标签: android ssl httpsurlconnection

尝试通过HTTPS向服务器发送POST请求时,我收到NullPointerException。这不是自签名证书(由CheapSSLs发布)。

这是我收到的错误:

06-21 18:52:37.757: E/AndroidRuntime(17985): FATAL EXCEPTION: AsyncTask #1
06-21 18:52:37.757: E/AndroidRuntime(17985): java.lang.RuntimeException: An error occured while executing doInBackground()
06-21 18:52:37.757: E/AndroidRuntime(17985):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-21 18:52:37.757: E/AndroidRuntime(17985):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
06-21 18:52:37.757: E/AndroidRuntime(17985):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
06-21 18:52:37.757: E/AndroidRuntime(17985):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
06-21 18:52:37.757: E/AndroidRuntime(17985):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-21 18:52:37.757: E/AndroidRuntime(17985):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-21 18:52:37.757: E/AndroidRuntime(17985):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-21 18:52:37.757: E/AndroidRuntime(17985):    at java.lang.Thread.run(Thread.java:1019)
06-21 18:52:37.757: E/AndroidRuntime(17985): Caused by: java.lang.NullPointerException
06-21 18:52:37.757: E/AndroidRuntime(17985):    at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:433)
06-21 18:52:37.757: E/AndroidRuntime(17985):    at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeConnection(HttpsURLConnectionImpl.java:378)
06-21 18:52:37.757: E/AndroidRuntime(17985):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
06-21 18:52:37.757: E/AndroidRuntime(17985):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614)
06-21 18:52:37.757: E/AndroidRuntime(17985):    at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:268)

我的代码如下:

URL url = new URL(mUrl);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.1.134", 8888));
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(proxy);

urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);

MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.build(params);

urlConnection.setRequestProperty("Content-Type",  "multipart/form-data; boundary=" + builder.mBoundary);
urlConnection.setFixedLengthStreamingMode(builder.getContentLength());

DataOutputStream out = new DataOutputStream(new BufferedOutputStream(urlConnection.getOutputStream()));

代码在getOutputStream上崩溃。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

尝试一下,不要在混合中包含proxy对象...... HttpsConnection应足以完成工作。