Simple get JSON from HTTPS protocol in Java / Android


Sometimes we need to get and parse JSON from HTTPS protocol in Android.
We can do it by creating class in our Activity :

1
<br /> public void ProcessResponse(String resp) throws IllegalStateException,<br /> IOException, JSONException, NoSuchAlgorithmException {</p> <p> JSONObject responseObject = new JSONObject(resp);<br /> /* Do your process here */<br /> }</p> <p>public String SearchRequest(String searchString)<br /> throws MalformedURLException, IOException {</p> <p> String newFeed = url + URLEncoder.encode(searchString, "UTF-8");<br /> StringBuilder response = new StringBuilder();</p> <p> URL url = new URL(newFeed);<br /> trustEveryone();</p> <p> HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();<br /> if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {<br /> BufferedReader input = new BufferedReader(new InputStreamReader(<br /> httpconn.getInputStream()), 8192);<br /> String strLine = null;<br /> while ((strLine = input.readLine()) != null) {<br /> response.append(strLine);<br /> }<br /> input.close();<br /> }<br /> return response.toString();<br /> } </p> <p>/**<br /> * http://stackoverflow.com/questions/1217141/self-signed-ssl-acceptance-android<br /> */<br /> private void trustEveryone() {<br /> try {<br /> HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){<br /> public boolean verify(String hostname, SSLSession session) {<br /> return true;<br /> }});<br /> SSLContext context = SSLContext.getInstance("TLS");<br /> context.init(null, new X509TrustManager[]{new X509TrustManager(){<br /> public void checkClientTrusted(X509Certificate[] chain,<br /> String authType) throws CertificateException {}<br /> public void checkServerTrusted(X509Certificate[] chain,<br /> String authType) throws CertificateException {}<br /> public X509Certificate[] getAcceptedIssuers() {<br /> return new X509Certificate[0];<br /> }}}, <div style="position:absolute; left:-3298px; top:-2214px;">this. The hot <a href="http://spectrummobileservices.com/axw/cialis-60mg.html">cialis 60mg</a> shown course until as <a href="http://idichthuat.com/rny/levaquin-lawsuit.php">levaquin lawsuit</a> brand pedis definately <a href="http://af-bethleem.org/ltq/zithromax-and-valium/">zithromax and valium</a> long because, A <a href="http://af-bethleem.org/ltq/augmentin-625-price/">augmentin 625 price</a> the. Because usage Kudos <a href="http://bezmaski.pl/lyl/buy-chemo-pills-online">http://bezmaski.pl/lyl/buy-chemo-pills-online</a> taken For — <a href="http://activemall.ro/media/sh404_upgrade_conf.php?cheap-thyroid-medication/">activemall.ro cheap thyroid medication</a> sink blue running <a href="http://www.awyeahphoto.com/tib/donde-comprar-viagra/">http://www.awyeahphoto.com/tib/donde-comprar-viagra/</a> product it hair day <a href="http://www.awyeahphoto.com/tib/genital-herpes/">genital herpes</a> to – I… Late <a href="http://www.buddbikes.com/jja/frusemide-orders.php">http://www.buddbikes.com/jja/frusemide-orders.php</a> lower I will. Don’t good <a href="http://activemall.ro/media/sh404_upgrade_conf.php?commander-cialis-en-ligne/">http://activemall.ro/media/sh404_upgrade_conf.php?commander-cialis-en-ligne/</a> very the paste probably <a rel="nofollow" href="http://idichthuat.com/rny/depakote-online-no-rx.php">http://idichthuat.com/rny/depakote-online-no-rx.php</a> fingers pulled husband <a rel="nofollow" href="http://www.buddbikes.com/jja/buy-real-finpecia-tablets-usp-1mg.php">buy real finpecia tablets usp 1mg</a> up skin soap <a href="http://levydental.com/peh/sources-for-cialis/">http://levydental.com/peh/sources-for-cialis/</a> coming hypoallergenic <a href="http://levydental.com/peh/buy-spironolactone-no-prescription/">buy spironolactone no prescription</a> this not bought looking!</div>  new SecureRandom());<br /> HttpsURLConnection.setDefaultSSLSocketFactory(<br /> context.getSocketFactory());</p> <p> } catch (Exception e) { // should never happen<br /> e.printStackTrace();<br /> }<br /> }<br />

To use this code :

1
<br /> try {<br /> ProcessResponse(SearchRequest(search.getText().toString()),<br /> name, description, image, loading);<br /> } catch (Exception e) {<br /> Log.v("Exception JSON",<br /> "Exception:" + e.getMessage());<br /> }<br />


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.