{"id":182,"date":"2015-02-02T10:38:32","date_gmt":"2015-02-02T09:38:32","guid":{"rendered":"https:\/\/mokrzycki.eu\/blog\/?p=182"},"modified":"2018-12-04T15:45:13","modified_gmt":"2018-12-04T14:45:13","slug":"working-example-post-in-android","status":"publish","type":"post","link":"https:\/\/mokrzycki.eu\/blog\/2015\/02\/02\/working-example-post-in-android\/","title":{"rendered":"Working example POST in android"},"content":{"rendered":"<p>Here is another short (IMHO) example how to post in android app.<\/p>\n<p>[java]<br \/>\nprivate void postSms(String text) {<br \/>\nURL url;<br \/>\nHttpURLConnection urlConnection = null;<\/p>\n<p>try {<br \/>\nurl = new URL(&#8220;http:\/\/yourserver.com\/sendsms&#8221;);<br \/>\nurlConnection = (HttpURLConnection) url.openConnection();<br \/>\nurlConnection.setRequestMethod(&#8220;POST&#8221;);<br \/>\nurlConnection.setDoOutput(true);<\/p>\n<p>List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();<br \/>\nparams.add(new BasicNameValuePair(&#8220;sms&#8221;, text));\/\/POST param as &#8220;sms&#8221;<\/p>\n<p>OutputStream os = urlConnection.getOutputStream();<br \/>\nBufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, &#8220;UTF-8&#8221;));<br \/>\nwriter.write(getQuery(params));<br \/>\nwriter.flush();<br \/>\nwriter.close();<br \/>\nos.close();<\/p>\n<p>String status = new String(Streams.getBytes(urlConnection.getInputStream()));<br \/>\nLog.d(TAG, new Exception().getStackTrace()[0].getMethodName() + &#8220;:insert status=&#8221; + status);<\/p>\n<p>} catch (MalformedURLException | ProtocolException e) {<br \/>\ne.printStackTrace();<br \/>\n} catch (IOException e) {<br \/>\ne.printStackTrace();<br \/>\n}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>This is a method for concatenating parameters for POST<\/p>\n<p><!--more--><\/p>\n<p>[java]<br \/>\nprivate String getQuery(List&lt;NameValuePair&gt; params) throws UnsupportedEncodingException {<br \/>\nStringBuilder result = new StringBuilder();<br \/>\nboolean first = true;<\/p>\n<p>for (NameValuePair pair : params) {<br \/>\nif (first)<br \/>\nfirst = false;<br \/>\nelse<br \/>\nresult.append(&#8220;&amp;&#8221;);<\/p>\n<p>result.append(URLEncoder.encode(pair.getName(), &#8220;UTF-8&#8221;));<br \/>\nresult.append(&#8220;=&#8221;);<br \/>\nresult.append(URLEncoder.encode(pair.getValue(), &#8220;UTF-8&#8243;));<br \/>\n}<\/p>\n<p>return result.toString();<br \/>\n}[\/java]<\/p>\n<p>Do not forget about adding permission in manifest!<br \/>\n[java]&lt;uses-permission android:name=&#8221;android.permission.INTERNET&#8221; \/&gt;[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is another short (IMHO) example how to post in android app. [java] private void&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[500],"tags":[5,123,120,122,83,119,126,118,121,117,125,124,35],"_links":{"self":[{"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/posts\/182"}],"collection":[{"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":3,"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":731,"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/posts\/182\/revisions\/731"}],"wp:attachment":[{"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}