Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use Both HTTPS and HTTP to parse JSON data in Android?

I followed this to Parse Json In Android

I have Successfully Done it with HttpData handler..

Here I am Successfully Posting Data to server and Getting Response..

Now I want to Use this same in the Part of 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...

Update In my code I have Changed HttpURLConnection to HttpsURLConnection

Please suggest me How to through this error In my code..

Update 1

I have Changed Certificate on server side.. Now its working On Https..

But Now,

I want to Use HTTP and HTTPS Both in one app Depending on Client Requirement So here now its worked with 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.

like image 332
Don't Be negative Avatar asked Mar 01 '26 13:03

Don't Be negative


1 Answers

to use both HTTP and HTTPS, you need to have the 2 methods (i think you already have them)

  1. GetHTTPData(String urlString)
  2. GetHTTPSData(String urlString)

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);
    }
}

now in the AsyncTask class ProcessJSON

replace this line stream = hh.GetHTTPData(urlString);

with this one stream = hh.GetDataFromUrl(urlString);

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)

like image 68
Yazan Avatar answered Mar 03 '26 02:03

Yazan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!