gpt4 book ai didi

Android sslpeerunverifiedexception 没有对等证书,JSON SSL

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

我在网上阅读了很多主题,主要是关于堆栈溢出。我仍然无法让我的代码正常工作。我仍然得到:Android sslpeerunverifiedexception no peer certificate

这是我的代码,如果有人能帮助我,我将不胜感激:

public class MainActivity extends Activity {

ArrayList<String> nameArrayList = new ArrayList<String>();
ArrayList<String> countryArrayList = new ArrayList<String>();

String graveUrl = "https://example.com/grave.json";


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new json().execute();

}

class json extends AsyncTask<Void, Void, String> {

protected void onPreExecute() {
super.onPreExecute();
}

protected String doInBackground(Void... params) {

HttpClient httpclient = createHttpClient();
HttpPost httppost = new HttpPost(graveUrl);

try {

HttpResponse response = httpclient.execute(httppost);

StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();

if (statusCode != 200) {
return null;
}

InputStream jsonStream = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(jsonStream));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}

String jsonData = builder.toString();
JSONObject json = new JSONObject(jsonData);
JSONArray name = json.getJSONArray("graves");

for (int i = 0; i < name.length(); i++) {
JSONObject video = name.getJSONObject(i);

nameArrayList.add(video.getString("ID_line"));
countryArrayList.add(video.getString("ID_grave"));
}

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

protected void onPostExecute(String result) {
super.onPostExecute(result);

Toast.makeText(getApplicationContext(),
"Hello" + nameArrayList.get(0).toString(),
Toast.LENGTH_LONG).show();

}
}

public static HttpClient createHttpClient() {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params,
HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);

SchemeRegistry schReg = new SchemeRegistry();

schReg.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));

schReg.register(new Scheme("https",
SSLSocketFactory.getSocketFactory(), 443));

ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
params, schReg);

return new DefaultHttpClient(conMgr, params);
}

}

我在堆栈溢出时得到的大部分代码,但看起来我无法将各个部分组合在一起以使其正常工作..

提前致谢

最佳答案

此服务器使用不受信任的证书,因此异常(exception)。您可以通过创建自己的 SSL 套接字工厂或在您的设备上添加证书来绕过此问题。

关于Android sslpeerunverifiedexception 没有对等证书,JSON SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26263246/

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