gpt4 book ai didi

java - 带有 DefaultHttpClient 和 POST over SSL 的 Android JSON 解析器

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:25 25 4
gpt4 key购买 nike

首先,抱歉我的英语不好!我的问题:我想通过这个 JSONParser.java 连接到受 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 代码而抛出。当未验证对等方的身份时抛出的异常。可能存在某些服务器端问题或您使用的 url 错误。因此,您无法连接到服务器。

检查这些

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

关于java - 带有 DefaultHttpClient 和 POST over SSL 的 Android JSON 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25602644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com