Denoteone
Denoteone

Reputation: 4055

How to use URLEncoder.encode android

How do you use URLEncoder.encode correctly?

When I encode my string to pass my parameters to a url the = and ? in my url is getting converted and breaking my php script.

This is my string that is getting encoded.

String spinnerURL = "getName.php?name=flowers";

Here is my script to encode the string and do a http request:

query = URLEncoder.encode(spinnerURL, "utf-8");
                Log.d(TAG, query.toString());
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
                try {

                    DefaultHttpClient httpClient = new DefaultHttpClient();

                    HttpGet httpPost = new HttpGet("http://www.website.com/tattoo/" + query);
                                  }

When I check my logCat for TAG I see the url looks like

getName.php%3Fname%3Dflowers

I left out the catch and a few other lines of code that do not effect the url ecode and http call.

Upvotes: 0

Views: 2572

Answers (1)

C. K. Young
C. K. Young

Reputation: 223023

That's by design. You're supposed to encode the getName.php, name, and flowers separately.

Upvotes: 1

Related Questions