3

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.

Don't Be negative
  • 1,215
  • 3
  • 19
  • 46
  • Https is different from http as it is safe and uses SSL – Nitesh Mar 19 '16 at 05:49
  • Recently My Server Changed to Https... So I want to Use Https in my activity... I am new to JSON please Suggest me On my Activity to Use Https without any major Changes... If Possible Give me a Example... I my Url if I give I am getting error.. for SSL... Because I am using http data handler.. suggest me for Https.. With that example code – Don't Be negative Mar 19 '16 at 05:53
  • Please read http://stackoverflow.com/questions/34293757/how-to-use-httpsurlconnection-instead-of-defaulthttpclient/34325483#34325483 to see if it can help or not – BNK Mar 19 '16 at 07:55
  • Thanks to all I Upvoted all of your answers – Don't Be negative Mar 31 '16 at 06:57

4 Answers4

3

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)

Yazan
  • 6,074
  • 1
  • 19
  • 33
1

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;
    }
Shripad Jadhav
  • 316
  • 1
  • 8
  • Once Check my code http://android--examples.blogspot.in/2015/05/how-to-parse-json-data-in-android.html and update your answer in it – Don't Be negative Mar 19 '16 at 06:41
  • Sir I am getting This error "**javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.**" But I type same URL on pc Firefox is broswer I got this "Your connection is not secure" after Allowing.. in advance I got result.. But what should I need to do know **Your Answer is working I think but not getting any response from server** Plese help me sir... – Don't Be negative Mar 19 '16 at 07:10
  • checkout this url, may you get some help http://developer.android.com/training/articles/security-ssl.html#CommonProblems – Shripad Jadhav Mar 19 '16 at 07:15
  • thanks @Shripad Jadhav ,, Now please check my updated Questions – Don't Be negative Mar 22 '16 at 07:13
1

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.

Ankur Samarya
  • 219
  • 1
  • 3
  • 15
1

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

pavan kvch
  • 153
  • 1
  • 13