Don't Be negative
Don't Be negative

Reputation: 1215

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.

Upvotes: 3

Views: 4406

Answers (4)

Yazan
Yazan

Reputation: 6082

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)

Upvotes: 3

pavan kvch
pavan kvch

Reputation: 153

Remove HttpDataHandler lines in doInBackground use HttpUrlConnection directly in doInBackground or use HttpUrlConnection in JSONparse class to post params to server follow this tutorial to post params Website

Upvotes: 1

Ankur Samarya
Ankur Samarya

Reputation: 229

What I understand is in your server side, they used self signed SSL certificate. So you have to install that certificate in your android device also. Settings > Security > install form storage.But for production build you have to buy ssl certificate from CA Authorities. Hope this will solve your problem.

Upvotes: 1

Shripad Jadhav
Shripad Jadhav

Reputation: 316

You can use HttpsURLConnection, replace HttpURLConnection by HttpsURLConnection .

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

Upvotes: 1

Related Questions