如何在Android中同时使用HTTPS和HTTP解析JSON数据?
我接着在Android中解析Json 我已经用HttpData处理程序成功地完成了 在这里,我成功地将数据发布到服务器并获得响应 现在,我想在HTTPS的部分使用相同的方法 有谁能建议我如何在代码中不做重大更改的情况下做到这一点。。因为在我的应用程序中,我这样做是为了更多的活动。。请建议我在代码中使用HTTPs 我会提供更多的信息。。。根据反应 更新 在我的代码中,我已将如何在Android中同时使用HTTPS和HTTP解析JSON数据?,android,json,http,https,Android,Json,Http,Https,我接着在Android中解析Json 我已经用HttpData处理程序成功地完成了 在这里,我成功地将数据发布到服务器并获得响应 现在,我想在HTTPS的部分使用相同的方法 有谁能建议我如何在代码中不做重大更改的情况下做到这一点。。因为在我的应用程序中,我这样做是为了更多的活动。。请建议我在代码中使用HTTPs 我会提供更多的信息。。。根据反应 更新 在我的代码中,我已将HttpURLConnection更改为HttpsURLConnection 请建议我如何通过我的代码中的此错误 更新1 我已
HttpURLConnection
更改为HttpsURLConnection
请建议我如何通过我的代码中的此错误
更新1
我已更改服务器端的证书。。现在它正在使用Https
但是现在,
我想在一个应用程序中使用HTTP和HTTPS,这取决于客户端的要求,所以现在它可以使用HTTPS
但我还需要使用Http
在我的代码中,任何人都可以建议我…我想我应该在一个应用程序中同时使用Https和Http。你可以使用HttpsURLConnection,用HttpsURLConnection替换HttpURLConnection
public String GetHTTPData(String urlString){
try{
URL url = new URL(urlString);
HttpsURLConnection urlConnection =(HttpsURLConnection)url.openConnection();
// Check the connection status
if(urlConnection.getResponseCode() == 200)
{
// if response code = 200 ok
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
// Read the BufferedInputStream
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
sb.append(line);
}
stream = sb.toString();
// End reading...............
// Disconnect the HttpURLConnection
urlConnection.disconnect();
}
else
{
// Do something
}
}catch (MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
}
// Return the data from specified url
return stream;
}
据我所知,在您的服务器端,他们使用了自签名SSL证书。因此,您还必须在android设备中安装该证书。设置>安全性>安装表单存储。但对于生产构建,您必须从CA机构购买ssl证书。
希望这能解决你的问题 我有相同的问题,所以我使用了此代码
// always verify the host - dont check for certificate
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier()
{
public boolean verify(String hostname, SSLSession session)
{
return true;
}
};
/**
* Trust every server - dont check for any certificate
*/
@SuppressLint("TrulyRandom")
private static void trustAllHosts()
{
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager()
{
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
根据您的要求更新代码。
public String postData(String urlString) throws JSONException, IOException
{
String result = "";
try
{
HttpURLConnection http = null;
URL url = new URL(urlString);
if (url.getProtocol().toLowerCase().equals("https"))
{
trustAllHosts();
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
http = https;
}
else {
http = (HttpURLConnection) url.openConnection();
}
Log.i("getDate", "="+http.getDate());
http.connect();
result = convertStreamToString((InputStream)http.getContent());
Log.i("tag", "="+result);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
static String convertStreamToString(java.io.InputStream is)
{
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
替换此行流=hh.GetHTTPData(urlString)
使用这个流=postData(urlString) 删除doInBackground中的HttpDataHandler行在doInBackground中直接使用HttpUrlConnection或在JSONparse类中使用HttpUrlConnection将参数发布到服务器按照本教程发布参数要同时使用HTTP和HTTPS,您需要有这两种方法(我认为您已经有了它们)
GetHTTPData(字符串urlString)
GetHTTPSData(字符串urlString)
HTTPDataHandler
类中(上面两种方法都有)
您需要创建第三个方法GetDataFromUrl()
,该方法将检查URL并决定使用哪种方法(http或https)
现在在AsyncTask类ProcessJSON
替换此行stream=hh.GetHTTPData(urlString)代码>
使用这个stream=hh.GetDataFromUrl(urlString)代码>
如果您不想在HTTPDataHandler
中添加第三个方法,只需使用ProcessJSON
中的if语句在doInBackground()
调用这两个方法中的任何一个(http或https)https与http不同,因为它是安全的,并且使用sslr最近我的服务器更改为https。。。所以我想在我的活动中使用Https。。。我是JSON新手,请建议我在活动中使用Https,无需任何重大更改。。。如果可能的话,给我举个例子。。。如果我给出了我的Url,我就会得到错误。。对于SSL。。。因为我正在使用http数据处理程序。。建议我使用Https。。对于该示例代码,请阅读,看看它是否有帮助。多亏了我对您的所有答案进行了投票。请检查我的代码并在其中更新您的答案。我收到了以下错误“javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:Trust anchor for certification path not found”但是我在pc上输入了相同的URL Firefox是broswer我在允许后得到了这个“你的连接不安全”。。我事先得到了结果。。但是我需要做什么知道你的答案是有效的我想但没有得到服务器的任何响应请帮助我先生…签出这个url,你可以得到一些帮助谢谢@Shripad Jadhav,,现在请检查我的更新问题Hanks@Ankur Samarya我已经做了关于CA当局的事情。。请检查我的最新问题。。对于使用HTTPS和Http两者,你能建议我如何在我的代码中使用它吗。。。在Http和Https的部分中,我已经完成了证书等。。。现在它可以通过这个使用Http或者通过这个使用https,你可以建议我在我的代码中使用这两种方式,,,使用上面的代码,它可以使用Http或者https。如果我的代码有任何问题,请告诉我。谢谢在我的代码中,我正在使用POST方法,如果你需要GET方法,你可以根据你的要求修改。谢谢@MPG,你能在我的代码中更新你的答案吗。。。检查这里请更新你的答案我会给你赏金。。我是网络服务新手。。。
public String GetDataFromUrl(String url){
if(url.toLowerCase().startsWith("https")){
//HTTPS:
return GetHTTPSData(url);
}else{
//HTTP:
return GetHTTPData(url);
}
}