URLEncoder encode / URLDecoder decode in java (Android)

The encoding parameter is the character encoding you're using. For example "UTF-8".


First you need to set the content-type as a 'x-www-form-urlencoded'. Then whatever content you would like to encode, encode it using "UTF-8".

For example:

For setting content to 'x-www-form-urlencoded':

URL url = new URL("http://www.xyz.com/SomeContext/SomeAction"); <br>
URLConnection urlConnection = url.openConnection();<br>
....<br>
....<br>
urlConnection.setRequestProperty("Content-type","application/x-www-form-urlencoded");


Or if you are using some JSP then you can write the following on top of it.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><br>
< META http-equiv="Content-Type" content="text/html; charset=UTF-8">


< FORM action="someaction.jsp" enctype="application/x-www-form-urlencoded" name="InputForm" method="POST">

And to use URLEncoder:

String encodedString = URLEncoder.encode("hello","UTF-8");

Tags:

Java

Android