3

I want to call a RestFul webservice in my android project. everything is Ok until I want to send special characters as a string to webservice.

at first I thought the problem is just with spaces and I replaced them with "%20". but I saw that these chars "\r\n,+,/,\,?,..." have problem too. I used URLEncoder.encodefor just that string( not whole URL) but it doesn't solve my problem for example it replaced spaces with '+' that caused error. or other exceptions for example when I have NewLine char in my string it gives me Bad Request - Invalid URL. and for each char I have some error like endpoint not found or Server Error in '/' Application.

    URL url = new URL(reqUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");

right now I replaced each char manually. I want it to know why URLEncoder.encode doesn't do the right way and is there any other way to solve it?

Solution:

I found my problem, from this document :

Template String Syntax

A template has three parts: a path, an optional query, and an optional fragment.

I was sending my string parameter as a path part not optional query. when I send URLEncoder.encode(myStringParameter) as an optional query I didn't have any problem even with spaces.

Maryam
  • 883
  • 10
  • 24

1 Answers1

0

You can use post method.

  1. Use GET for safe actions and POST for unsafe actions.

  2. Use POST when dealing with sensitive data.

  3. Use POST when dealing with long requests.

  4. Use GET in AJAX environments.

Extra info : link

Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98