2

i have an error while i tried to run some spaces in url android, this is the code :

              String strUrlLoginFullpath = "";


              strUrlLoginFullpath = strUrlMain+"exl?u="+strUser+"&p="+strPass+"&t=1";



              URL url = new URL(strUrlLoginFullpath);
              publishProgress(40);

              //membuka koneksi
              URLConnection urlConn = url.openConnection();
              urlConn.setConnectTimeout(50000);
              urlConn.setReadTimeout(50000);

              //digunakan untuk menangkap nilai dari server
              BufferedReader in = new BufferedReader(
                      new InputStreamReader(
                              urlConn.getInputStream()));
              publishProgress(60);
              String inputLine;
              int i=0;
              while ((inputLine = in.readLine()) != null)
              {     
                  Log.v("Login",i+", return:" +inputLine+"; url: "+strUrlLoginFullpath);
                  if(i == 5)
                      {
                        Log.v("Login","RESULT >"+i+", return:" +inputLine+"; url: "+strUrlLoginFullpath);
                        str2 = inputLine;
                      }
                  i++;
              }
              in.close();


              publishProgress(80);

              publishProgress(100);
          } 
          catch (MalformedURLException e) 
          {
                Log.v("KxL", "MalformedURLException=" + e.toString());
          } 
          catch (IOException e) 
          {
                Log.v("KxL", "IOException=" + e.toString());
          } 
          return null;
    }

as you see.. i have the command for take login validation in strUrlLoginFullpath = strUrlMain+"exl?u="+strUser+"&p="+strPass+"&t=1"; but there's condition that strUser sometimes containing blank spaces, that can make my program doesn't run. so how the way to solve this problem.?

Egor
  • 39,695
  • 10
  • 113
  • 130
Aoyama Nanami
  • 2,532
  • 9
  • 32
  • 50

4 Answers4

3

URLEncoder.encode() your URL parameter values so that spaces and other special characters get correctly encoded.

Example:

strUrlLoginFullpath = strUrlMain + "exl?u=" + URLEncoder.encode(strUser, "UTF-8") +
    "&p=" + URLEncoder.encode(strPass, "UTF-8") + "&t=1";
laalto
  • 150,114
  • 66
  • 286
  • 303
1

Spaces aren't allowed in URLs. Instead, you should URL encode them - replace all the spaces with %20 and things should come out right in the other end.

If there is any chance any other special character (as considered special in URLs) might be in any of those strings, which for passwords at least seems quite likely, you should really URL encode the whole string using a suitable URL encoding function to avoid any problems or security issues.

Ignoring, that is, the security issue of passing a password in a URL's query string.

Matthew Walton
  • 9,809
  • 3
  • 27
  • 36
0

try using the URLEncoder.encode()

strUrlLoginFullpath = strUrlMain+"exl?u="+ URLEncoder.encode(strUser)+"&p="+URLEncoder.encode(strPass)+"&t=1";
Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
0

After creating the url String just do this:

strUrlLoginFullpath = strUrlMain+"exl?u="+strUser+"&p="+strPass+"&t=1"; strUrlLoginFullpath.replace(" ", "20%")