带有DefaultHttpClient和POST的SSL JSON解析器通过SSL
首先,对不起我的英语不好!我的问题:用这个JSONParser.java我想连接到一个SSL安全的服务器。如何更改我的代码以使其工作?带有DefaultHttpClient和POST的SSL JSON解析器通过SSL
此外:SSL证书是自签证书!
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {}
public JSONObject getJSONFromUrl(String url, List params) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {sb.append(line).append("n");}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
logcat的输出(我不得不审查的软件包名):
09-01 11:26:14.759 1462-1462/ *package_name* W/ActivityThread: Application *package_name* is waiting for the debugger on port 8100...
09-01 11:26:14.806 1462-1462/ *package_name* I/System.out: Sending WAIT chunk
09-01 11:26:14.887 1462-1467/ *package_name* I/dalvikvm: Debugger is active
09-01 11:26:15.031 1462-1462/ *package_name* I/System.out: Debugger has connected
09-01 11:26:15.031 1462-1462/ *package_name* I/System.out: waiting for debugger to settle...
09-01 11:26:17.673 1462-1462/ *package_name* I/System.out: debugger has settled (1391)
09-01 11:26:21.538 1462-1462/ *package_name* W/ResourceType: Entry identifier 0x119 is larger than entry count 0xab
09-01 11:26:28.164 1462-1462/ *package_name* W/KeyCharacterMap: No keyboard for id 0
09-01 11:26:28.164 1462-1462/ *package_name* W/KeyCharacterMap: Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-01 11:26:44.737 1462-1462/ *package_name* D/dalvikvm: GC_EXTERNAL_ALLOC freed 164K, 50% free 2797K/5575K, external 3181K/3266K, paused 74ms
09-01 11:26:57.325 1462-1475/ *package_name* W/System.err: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
09-01 11:26:57.349 1462-1475/ *package_name* W/System.err: at org.apache.harmony.xnet.provider.jsse.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:258)
09-01 11:26:57.349 1462-1475/ *package_name* W/System.err: at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)
09-01 11:26:57.423 1462-1475/ *package_name* W/System.err: at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)
09-01 11:26:57.447 1462-1475/ *package_name* W/System.err: at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:164)
09-01 11:26:57.473 1462-1475/ *package_name* W/System.err: at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-01 11:26:57.473 1462-1475/ *package_name* W/System.err: at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-01 11:26:57.533 1462-1475/ *package_name* W/System.err: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
09-01 11:26:57.556 1462-1475/ *package_name* W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-01 11:26:57.556 1462-1475/ *package_name* W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-01 11:26:57.608 1462-1475/ *package_name* W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-01 11:26:57.608 1462-1475/ *package_name* W/System.err: at *package_name*.library.JSONParser.getJSONFromUrl(JSONParser.java:36)
09-01 11:26:57.608 1462-1475/ *package_name* W/System.err: at *package_name*.library.UserFunctions.loginUser(UserFunctions.java:38)
09-01 11:26:57.656 1462-1475/ *package_name* W/System.err: at *package_name*.Login$ProcessLogin.doInBackground(Login.java:214)
09-01 11:26:57.682 1462-1475/ *package_name* W/System.err: at *package_name*.Login$ProcessLogin.doInBackground(Login.java:194)
09-01 11:26:57.682 1462-1475/ *package_name* W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-01 11:26:57.706 1462-1475/ *package_name* W/System.err: at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
09-01 11:26:57.706 1462-1475/ *package_name* W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:138)
09-01 11:26:57.706 1462-1475/ *package_name* W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
09-01 11:26:57.741 1462-1475/ *package_name* W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
09-01 11:26:57.741 1462-1475/ *package_name* W/System.err: at java.lang.Thread.run(Thread.java:1019)
09-01 11:26:57.741 1462-1475/ *package_name* E/Buffer Error: Error converting result java.lang.NullPointerException
09-01 11:26:57.869 1462-1475/ *package_name* E/JSON Parser: Error parsing data org.json.JSONException: End of input at character 0 of
09-01 11:26:57.892 1462-1462/ *package_name* D/AndroidRuntime: Shutting down VM
09-01 11:26:57.892 1462-1462/ *package_name* W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-01 11:26:58.016 1462-1462/ *package_name* E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at *package_name*.Login$ProcessLogin.onPostExecute(Login.java:219)
at *package_name*.Login$ProcessLogin.onPostExecute(Login.java:194)
at android.os.AsyncTask.finish(AsyncTask.java:417)
at android.os.AsyncTask.access$300(AsyncTask.java:127)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
SSLPeerUnverifiedExcption没有抛出,因为当对方的身份一直没有被抛出你的Android code.The异常verified.There可能有一些服务器端问题,或者您使用的是一个错误的网址。因此,您无法连接服务器。
检查这些
Android ssl: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate (yet again)
Safely fixing: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
javax.net.ssl.sslpeerunverifiedexception no peer certificate
我编辑了我的代码,但错误仍然存在..我认为这是因为它是自签名证书 – 2014-09-01 10:07:50
经过一些更改后,Logcat说'javax.net.ssl.SSLException:连接被对等关闭' – 2014-09-01 10:41:05
是ü面临什么问题?错误?异常? – 2014-09-01 09:24:43
该应用程序崩溃和Logcat说: 'W/System.err:javax.net.ssl.SSLPeerUnverifiedException:没有对等证书' 'E/Buffer Error:转换结果java.lang.NullPointerException'错误 'E/JSON解析器:解析数据时出错org.json.JSONException:在' – 2014-09-01 09:29:11
@atom_dad的字符0输入结束请添加您的logcat本身。 – SilentKiller 2014-09-01 09:34:52