我在网上阅读了很多主题,主要是关于堆栈溢出..我仍然无法让我的代码正常运行..我仍然得到: Android sslpeerunverifiedexception没有对等证书
这是我的代码,如果有人可以帮助我,我将不胜感激:
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);
}
}
大部分代码都是我在堆栈溢出时得到的,但看起来我不能把它们放在一起才能正常工作..
提前谢谢
答案 0 :(得分:0)
此服务器使用不受信任的证书,因此例外。您可以通过创建自己的SSL套接字工厂或在设备上添加证书来绕过此问题。
答案 1 :(得分:0)
最后看起来我的代码出了问题,因为这个代码一切正常:
class UpdateTask extends AsyncTask<Void, Void, String> {
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(Void... params) {
URL url;
try {
url = new URL("https://url.com");
URLConnection urlConnection = url.openConnection();
InputStream in = urlConnection.getInputStream();
String jsonData = convertStreamToString(in);
JSONObject json = new JSONObject(jsonData);
JSONArray name = json.getJSONArray("graves");
for (int i = 0; i < name.length(); i++) {
JSONObject video = name.getJSONObject(i);
lineArrayList.add(video.getString("ID_line"));
graveArrayList.add(video.getString("ID_grave"));
}
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}