0

I'm trying to do an http request in java but I get this error:

java.lang.IllegalArgumentException: Invalid % sequence: %Ne in query at index 80:

try {
     HttpClient Client = new DefaultHttpClient();
     String URL = "Example.com";
     HttpGet httpget = new HttpGet(URL);
     ResponseHandler<String> responseHandler = new BasicResponseHandler();
     String SetServerString = Client.execute(httpget, responseHandler);
     Log.e("iets",SetServerString);

} 
catch (ClientProtocolException e) {
     e.printStackTrace();
} 
catch (IOException e) {
     e.printStackTrace();
}

It seems I cant use the character '%' in the query. But the thing is that I really need to use the '%' since it represents a space in my echonest.php.

How can I solve this problem?

thanks in advance

3
  • A space is writer as %20 in a query. Commented Apr 30, 2015 at 8:12
  • Have you tried encoding the url? URLEncoder.encode(URL, "utf-8"); Commented Apr 30, 2015 at 8:13
  • You may want to use URI templates; I happen to have a library for that, but others also exist. In particular, if you use Guava, it has Escapers for that. Commented Apr 30, 2015 at 8:25

4 Answers 4

2

% is used to encode special characters in URLs. To encode the actual "%" character you need to use %25 The url will become: http://bertenbevers.be/ProjectICT4/echonest.php?title=stormwatch&artist=Helvetic%25Nerds

There are ready made libraries to encode urls. It will help avoid other strange symbols like spaces, slashes etc.

Sign up to request clarification or add additional context in comments.

Comments

1

You should encode your URL using URLEncoder.

String URL = "http://bertenbevers.be/ProjectICT4/echonest.php?title=stormwatch&artist=" + URLEncoder.encode("Helvetic%Nerds", "UTF-8");

1 Comment

Didn't downvote but you're encoding too much, such as the = and &. Many other answers have the same problem.
0

Use URLEncoder like this:

String URL = "www.example.com"?title=stormwatch&artist=Helvetic"+URLEncoder.encode("%","UTF-8")+"Nerds";

Modify your code:

try {
                HttpClient Client = new DefaultHttpClient();
                String URL = "www.example.com/?title=stormwatch&artist=Helvetic"+URLEncoder.encode("%","UTF-8")+"Nerds";
                HttpGet httpget = new HttpGet(URL);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String SetServerString = Client.execute(httpget, responseHandler);
                Log.e("iets",SetServerString);

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

1 Comment

+"Nerds" - you've forgotten to put that under double quotes.
-1

Since you are using a special character in your URL you should user URLEncoder.encode(<your URL>) method

for example :- String url = "http://bertenbevers.be/ProjectICT4/echonest.php?"+URLEncoder.encode("title=stormwatch&artist=Helvetic%Nerds");

Hope this might help you in solving your problem

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.