{"id":189,"date":"2011-08-23T10:14:30","date_gmt":"2011-08-23T14:14:30","guid":{"rendered":"http:\/\/digitallibraryworld.com\/?p=189"},"modified":"2014-03-31T18:01:04","modified_gmt":"2014-03-31T22:01:04","slug":"http-or-https-post-in-android-using-httpurlconnection","status":"publish","type":"post","link":"https:\/\/heisbudi.com\/?p=189","title":{"rendered":"HTTP or HTTPS Post in Android using HTTPURLConnection"},"content":{"rendered":"<p>I&#8217;m starting to dive deep into Android world. The first step is usually the hardest. I&#8217;m starting to grasp how the Android environment operates. The best way for me to learn is by reading, and actually doing it. So, I started to build a room reservation app using an web service API built by my co-worker. Calling this API requires HTTP POST call from Android. This is where it gets interesting. I tried scouring the web for hours, and all the example I found did not work! I finally looked into <a href=\"http:\/\/developer.android.com\/reference\/java\/net\/HttpURLConnection.html\">android documentation<\/a>, which is really where I should started first. It seems so simple. I could do it in 10 seconds in c#. However during my trial, all the post parameters are not recognized by the API, which is written using PHP. I&#8217;ll describe some of the mistake I made.<\/p>\n<p>\/\/import these on your header<\/p>\n<div id=\"_mcePaste\"><span style=\"color: #800080;\">import java.io.IOException;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #800080;\">import java.io.PrintWriter;<br \/>\n<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #800080;\">import java.net.HttpURLConnection;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #800080;\">import java.net.MalformedURLException;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #800080;\">import java.net.ProtocolException;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #800080;\">import java.net.URL;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #800080;\">import java.net.URLEncoder;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #800080;\">import java.util.Scanner;<\/span><\/div>\n<p>\/\/do this wherever you are wanting to POST<br \/>\n<span style=\"color: #800080;\"> URL url;<br \/>\nHttpURLConnection conn;<!--more--><br \/>\n<\/span><\/p>\n<p><span style=\"color: #800080;\">try{<\/span><br \/>\n\/\/if you are using https, make sure to import java.net.HttpsURLConnection<br \/>\n<span style=\"color: #800080;\"> url=new URL(&#8220;http:\/\/somesite\/somefile.php&#8221;);<\/span><\/p>\n<p>\/\/you need to encode ONLY the values of the parameters<br \/>\n<span style=\"color: #800080;\"> String param=&#8221;param1=&#8221; + URLEncoder.encode(&#8220;value1&#8243;,&#8221;UTF-8&#8221;)+<br \/>\n&#8220;&amp;param2=&#8221;+URLEncoder.encode(&#8220;value2&#8243;,&#8221;UTF-8&#8221;)+<br \/>\n&#8220;&amp;param3=&#8221;+URLEncoder.encode(&#8220;value3&#8243;,&#8221;UTF-8&#8221;);<\/span><\/p>\n<p><span style=\"color: #800080;\">conn=(HttpURLConnection)url.openConnection();<\/span><br \/>\n\/\/set the output to true, indicating you are outputting(uploading) POST data<br \/>\n<span style=\"color: #800080;\"> conn.setDoOutput(true);<\/span><br \/>\n\/\/once you set the output to true, you don&#8217;t really need to set the request method to post, but I&#8217;m doing it anyway<br \/>\n<span style=\"color: #800080;\"> conn.setRequestMethod(&#8220;POST&#8221;);<\/span><\/p>\n<p>\/\/Android documentation suggested that you set the length of the data you are sending to the server, BUT<br \/>\n\/\/ do NOT specify this length in the header by using conn.setRequestProperty(&#8220;Content-Length&#8221;, length);<br \/>\n\/\/use this instead.<br \/>\n<span style=\"color: #800080;\">conn.setFixedLengthStreamingMode(param.getBytes().length);<\/span><br \/>\n<span style=\"color: #800080;\">conn.setRequestProperty(&#8220;Content-Type&#8221;, &#8220;application\/x-www-form-urlencoded&#8221;);<\/span><br \/>\n\/\/send the POST out<br \/>\n<span style=\"color: #800080;\">PrintWriter out = new PrintWriter(conn.getOutputStream());<br \/>\nout.print(param);<br \/>\nout.close();<\/span><\/p>\n<p>\/\/build the string to store the response text from the server<br \/>\n<span style=\"color: #800080;\"> String response= &#8220;&#8221;;<\/span><\/p>\n<p>\/\/start listening to the stream<br \/>\n<span style=\"color: #800080;\">Scanner inStream = new Scanner(conn.getInputStream()); <\/span><\/p>\n<p>\/\/process the stream and store it in StringBuilder<br \/>\n<span style=\"color: #800080;\">while(inStream.hasNextLine())<br \/>\n<\/span><span style=\"color: #800080;\"> response+=(inStream.nextLine());<\/span><\/p>\n<p><span style=\"color: #800080;\"><span style=\"color: #800080;\">}<br \/>\n<span style=\"color: #000000;\">\/\/catch some error<\/span><br \/>\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">catch(MalformedURLException ex){<\/span><\/span><br \/>\n<\/span><span style=\"color: #800080;\"> Toast.makeText(MyActivity.this, ex.toString(), 1 ).show();<\/span><span style=\"white-space: pre;\"><br \/>\n<\/span><span style=\"color: #800080;\">}<br \/>\n<span style=\"color: #000000;\">\/\/ and some more<\/span><br \/>\n<\/span><span style=\"color: #800080;\">catch(IOException ex){<\/span><\/span><\/p>\n<p><span style=\"color: #800080;\"> Toast.makeText(MyActivity.this, ex.toString(), 1 ).show();<br \/>\n<\/span><span style=\"color: #800080;\"> } <\/span><\/p>\n<p><span style=\"color: #000000;\">That&#8217;s it. Whatever output you receive from the URL you are calling would be stored in <span style=\"color: #800080;\">response<\/span> string.<\/span><\/p>\n<div data-counters='1' data-style='square' data-size='regular' data-url='https:\/\/heisbudi.com\/?p=189' data-title='HTTP or HTTPS Post in Android using HTTPURLConnection' class='linksalpha_container linksalpha_app_3'><a href='\/\/www.linksalpha.com\/share?network='facebook' class='linksalpha_icon_facebook'><\/a><a href='\/\/www.linksalpha.com\/share?network='twitter' class='linksalpha_icon_twitter'><\/a><a href='\/\/www.linksalpha.com\/share?network='googleplus' class='linksalpha_icon_googleplus'><\/a><a href='\/\/www.linksalpha.com\/share?network='mail' class='linksalpha_icon_mail'><\/a><\/div><div data-position='' data-url='https:\/\/heisbudi.com\/?p=189' data-title='HTTP or HTTPS Post in Android using HTTPURLConnection' class='linksalpha_container linksalpha_app_7'><a href='\/\/www.linksalpha.com\/share?network='facebook' class='linksalpha_icon_facebook'><\/a><a href='\/\/www.linksalpha.com\/share?network='twitter' class='linksalpha_icon_twitter'><\/a><a href='\/\/www.linksalpha.com\/share?network='googleplus' class='linksalpha_icon_googleplus'><\/a><a href='\/\/www.linksalpha.com\/share?network='mail' class='linksalpha_icon_mail'><\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>Doing HTTP POST or HTTPS POST in android using HttpUrlConnection<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[38],"_links":{"self":[{"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/posts\/189"}],"collection":[{"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/heisbudi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=189"}],"version-history":[{"count":8,"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/posts\/189\/revisions"}],"predecessor-version":[{"id":477,"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/posts\/189\/revisions\/477"}],"wp:attachment":[{"href":"https:\/\/heisbudi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/heisbudi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/heisbudi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}