如何在Android中同时使用HTTPS和HTTP解析JSON数据? [英] How to Use Both HTTPS and HTTP to parse JSON data in Android?
问题描述
我关注了此解析Android中的Json
I followed this to Parse Json In Android
我已经使用 HttpData处理程序成功完成了此操作.
I have Successfully Done it with HttpData handler..
我在这里成功地将数据发布到服务器并获得响应.
Here I am Successfully Posting Data to server and Getting Response..
现在,我想在 HTTPS 的部分中使用此名称..
Now I want to Use this same in the Part of HTTPS..
任何人都可以建议我如何在不对我的代码进行重大更改的情况下执行此操作..因为在我的应用程序中,我正在这样做以进行更多活动..请建议我在我的代码中使用 HTTPs .
Can Any one suggest me How to do this Without Major Changes in my code.. Because In my application I am doing this for more activities.. Please Suggest me to Use HTTPs in my code..
我将提供其他信息...取决于响应...
I will provide Additional Info... Depending Responses...
更新
在我的代码中,我已将HttpURLConnection
更改为HttpsURLConnection
Update
In my code I have Changed HttpURLConnection
to HttpsURLConnection
请建议我如何解决此错误.
Please suggest me How to through this error In my code..
更新1
我已经在服务器端更改了证书..现在它可以在Https上工作了.
I have Changed Certificate on server side.. Now its working On Https..
但是现在,
我想在一个应用程序中同时使用HTTP和HTTPS,具体取决于客户端要求,因此,现在这里可以与Https一起使用....
I want to Use HTTP and HTTPS Both in one app Depending on Client Requirement So here now its worked with Https....
但是我还需要使用Http 在我的代码中,任何人都可以建议我...我希望我应该在一个应用程序中同时使用Https和Https.
But I also need to work with Http In my Code Can any any one suggest me...I want I should Work with Https and Http Both In one App.
推荐答案
要同时使用HTTP和HTTPS,您需要具有2种方法(我认为您已经拥有它们)
to use both HTTP and HTTPS, you need to have the 2 methods (i think you already have them)
-
GetHTTPData(String urlString)
-
GetHTTPSData(String urlString)
GetHTTPData(String urlString)
GetHTTPSData(String urlString)
现在在HTTPDataHandler
类中(您同时具有上述两种方法)
您需要创建第三个方法GetDataFromUrl()
,该方法将检查URL并确定要使用哪种方法(http或https)
now in HTTPDataHandler
class (where you have both methods above)
you need to create a 3rd method GetDataFromUrl()
, that will check URL and decide which method to use (http or https)
public String GetDataFromUrl(String url){
if(url.toLowerCase().startsWith("https")){
//HTTPS:
return GetHTTPSData(url);
}else{
//HTTP:
return GetHTTPData(url);
}
}
现在在AsyncTask类ProcessJSON
now in the AsyncTask class ProcessJSON
替换此行stream = hh.GetHTTPData(urlString);
与此stream = hh.GetDataFromUrl(urlString);
如果您不想在HTTPDataHandler
中添加该第三种方法,只需在doInBackground()
处的ProcessJSON
中使用if-statement
来调用这两种方法(http或https)之一
if you don't want to add that 3rd method in HTTPDataHandler
, just use the if-statement
in ProcessJSON
at doInBackground()
to call either one of the 2 methods (http or https)
这篇关于如何在Android中同时使用HTTPS和HTTP解析JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!