0

I want a url that will look like this

"http://example.com/get_item_data.php?uid="inv_no"

I tried URL encoder, but could not figure it out,

I first tried this, it did not work

try {
   json_url = "http://example.com/get_item_data.php?uid="+  URLEncoder.encode(inv_no, "UTF-8");
} catch (UnsupportedEncodingException e) {
   e.printStackTrace();
}

then this

try {
   json_url = "http://example.com/get_item_data.php?uid="+ URLEncoder.encode("inv_no", "UTF-8") + "=" + URLEncoder.encode(inv_no, "UTF-8");
} catch (UnsupportedEncodingException e) {
   e.printStackTrace();
}

yet, no result, please help

freedev
  • 25,946
  • 8
  • 108
  • 125
LaideLawal
  • 77
  • 1
  • 10

3 Answers3

0

Encode the whole string AFTER you added your ´inv_no´-variable.

try { json_url = URLEncoder.encode('http://example.com/get_item_data.php?uid='+inv_no, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }

Kai Adelmann
  • 199
  • 6
  • 15
0

Usually you don't have to quote your parameters. So to me your first trial is correct:

   json_url = "http://example.com/get_item_data.php?uid="+ 
                  URLEncoder.encode( inv_no, "UTF-8");

Not sure about what you're trying to do, but looking at your question I suggest the following code:

try {
   json_url = "http://example.com/get_item_data.php?uid=\""+ 
                  URLEncoder.encode( inv_no, "UTF-8") + "\"";
} catch (UnsupportedEncodingException e) {
   e.printStackTrace();
}

In this case you have the quote outside the encoded parameter inv_no.

Or even:

try {
   json_url = "http://example.com/get_item_data.php?uid="+ 
                  URLEncoder.encode("\"" + inv_no + "\"", "UTF-8");
} catch (UnsupportedEncodingException e) {
   e.printStackTrace();
}

In this case the parameter inv_no and the quotes will be encoded.

freedev
  • 25,946
  • 8
  • 108
  • 125
0
try {
       String    json_url = "\"http://example.com/get_item_data.php?uid=\"inv_no\"";
     System.out.println(json_url);  
     } catch (Exception e) {
           e.printStackTrace();
        }
codehacker
  • 381
  • 5
  • 15