1

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();
        }


}
4
  • 2
    you can replace the space with %20 Commented Sep 15, 2017 at 6:23
  • spaces are allowed because if you use a URL with spaces from your code it will read as %20. so you won't be having any issues in strings with spaces used as URL Commented Sep 15, 2017 at 6:25
  • But, if i dont use space in edittext value then its work perfectly and when i use space its not work Commented Sep 15, 2017 at 6:27
  • Possible duplicate of Replacing spaces with %20 in java Commented Sep 15, 2017 at 6:38

1 Answer 1

4

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")

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

2 Comments

You could do something like this: "http://"+ip+":"+port+"/unitnames.cgi?outname1="+name1.replace(" ", "%20")+""
it give me error like "You cannot resolve method replace(java.jang.Stirng,java.lang,Stirng)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.