Reputation: 1351
I have a url that one of its params contains a space character. If I send it as is, using HttpURLConnection, this param passes wrongly.
If I manually replace the space with %20, it's working as expected so I was wondering if there is a cleaner way to do so though I was hoping that HttpURLConnection will do it automatically. Maybe there is a way that I missed?
When looking for this, I keep bumping into URLEncoder.Encode which is deprecated and I couldn't find any other way to do what I do except for encoding the whole URL, including the :// of the http.
Is there a clean way to do the replacement or should I do it manually?
url for example: http://www.domain.com?param1=name'last¶m2=2014-31-10 11:40:00 param 1 contains ' and param2 contains both space and : but only the space makes the problem. This is why I don't understand why HttpUrlConnection is so sensitive for space only.
Thanks
Upvotes: 6
Views: 20161
Reputation: 242
Instead of using HttpURLConnection why don't you use HttpClient class with HttpPost class. This way you avoid the tension of URL encoding. Below is an example on how to it:
String result = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(YOUR URL);
try {
ArrayList<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
nameValuePairs.add(new BasicNameValuePair(PARAMETER1, VALUE1));
nameValuePairs.add(new BasicNameValuePair(PARAMETER2, VALUE2));
...
nameValuePairs.add(new BasicNameValuePair(PARAMETERn, VALUEn));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
result = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
EDIT:
String result = "";
URL url = new URL(YOUR URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(PARAMETER1, VALUE1));
nameValuePairs.add(new BasicNameValuePair(PARAMETER2, VALUE2));
...
nameValuePairs.add(new BasicNameValuePair(PARAMETERn, VALUEn));
//Send request
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getUrlParameters(params));
writer.flush();
writer.close();
os.close();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = br.readLine()) != null) {
response.append(line);
response.append('\r');
}
r.close();
result = response.toString();
conn.disconnect();
Code for getUrlParameters() method:
private String getUrlParameters(List<NameValuePair> params) throws UnsupportedEncodingException{
StringBuilder parameter = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
parameter.append(URLEncoder.encode(pair.getName(), "UTF-8"));
parameter.append("=");
parameter.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
parameter.append("&");
}
return parameter.toString().substring(0, parameter.length()-2);
}
But my initial approach is very easy to implement and has never failed me not even once. As for which approach you wish to use totally up to you, weather you go with HttpURLConnection (Android recommended approach) or the other one.
Upvotes: -3
Reputation: 24848
Try this way,hope this will help you to solve your problem.
URLEncoder : All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9') and characters '.', '-', '*', '_' are converted into their hexadecimal value prepended by '%'.
In URLEncoder class have two method :
1.encode(String url) : This method was deprecated in API level 1
String encodedUrl = URLEncoder.encode(url);
2.encode(String url, String charsetName) : Encodes url using the Charset named by charsetName.
String encodedUrl = URLEncoder.encode(url,"UTF-8");
How to use :
String url ="http://www.domain.com";
String param1 ="?param1=";
Strinf param1value ="name'last";
String param2 ="¶m2=";
Strinf param2value ="2014-31-10 11:40:00";
String encodeUrl = url +param1+ URLEncoder.encode(param1value,"UTF-8")+param2+URLEncoder.encode(param2value,"UTF-8");
Upvotes: 7
Reputation: 719
As stated in the URLEncoder's javadoc, you should use URLEncoder.encode(String s, String enc)
Upvotes: 0
Reputation: 2088
you can use
String oldurl="http://pairdroid.com/whatsapp.php?a=rajesh saini";
String newurl=oldurl.replaceAll(" ","%20");
URL url = new URL(newurl);
Upvotes: 4