Devyani Kotadiya
Devyani Kotadiya

Reputation: 450

How to allow space in url with string variable in android?

I want to set edittext valu's string variable in url with space. I get edittext value in string with space.But, this string variable with space can;t use in url. So, I want to use this string variable with space in url.

I use this button click listener and get string value from edittext.I get String variable completely with space,

  btnoutname1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            name1=edtoutname1.getText().toString();
            Log.e("name1: ", "> " + name1);


            change1();

        }
    });

Now, I want to use this string variable with space in url.you can see in this method I use name1 string variable in url.But when i set space in name1 string then i cant update. So, How i can allow space in this url

    public static String change1() {
            StringBuffer stringBuffer = new StringBuffer("");
            BufferedReader bufferedReader = null;
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet();

                URI uri = new URI("http://"+ip+":"+port+"/unitnames.cgi?outname1="+name1+"");

                 Log.e("name1: ", "> " + name1);

                httpGet.setURI(uri);
                httpGet.addHeader(BasicScheme.authenticate(
                        new UsernamePasswordCredentials(uname, password),
                        HTTP.UTF_8, false));

                HttpResponse httpResponse = httpClient.execute(httpGet);
                InputStream inputStream = httpResponse.getEntity().getContent();
                bufferedReader = new BufferedReader(new InputStreamReader(
                        inputStream));

                String readLine = bufferedReader.readLine();
                while (readLine != null) {
                    stringBuffer.append(readLine);
                    stringBuffer.append("\n");
                    readLine = bufferedReader.readLine();
                }
            } catch (Exception e) {
                // TODO: handle exception
            } finally {
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        // TODO: handle exception
                    }
                }
            }
            return stringBuffer.toString();
        }


}

Upvotes: 1

Views: 1657

Answers (1)

Sammekl
Sammekl

Reputation: 531

You are probably looking for a way to url encode your String. This can be done using the URLEncoder.

You could also manually replace the space character the using String url = urlString.replace(" ", "%20")

Upvotes: 4

Related Questions