如何在android中的volley调用中传递url中的特殊字符

时间:2017-04-04 06:19:07

标签: java android android-volley

String description="60% off";

String url_offerAdd=UrlString.url_string+"/offer_Add.php?email="+email+"&desc="+description;

url_offerAdd.replace(" ","%20");

## Heading ##StringRequest  stringRequest=new StringRequest(Request.Method.GET, url_offerAdd, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
Log.v("response",response);
                    Toast.makeText(getBaseContext(),"Offer is submitted succcessfully",Toast.LENGTH_LONG).show();
                    desc.setText("");
                    tvToDate.setText("");
                    desc.setFocusable(false);
                    tvFromDate.setText("");
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.v("response1",error.getMessage());
                }
            });
            MySingleton.getInstance(getApplicationContext()).addToRequestque(stringRequest);

因为我已经描述了60%的折扣,所以每当我调用php webservice时,%符号都没有传递url同样没有特殊字符传递。

3 个答案:

答案 0 :(得分:0)

对网址进行编码

String encodedUrl = URLEncoder.encode(url, "UTF-8");

答案 1 :(得分:0)

你可以尝试这种方法,对我来说很好。

  String urll ="http://www.google.com/methodname?Userid="+user_id;

    String url = Uri.parse(urll)
            .buildUpon()
            .appendQueryParameter("Title", title)
            .appendQueryParameter("Desc", description)
            .appendQueryParameter("embededvideo", embededvideo)
            .appendQueryParameter("location", location)
            .appendQueryParameter("perferances", perferances)
            .appendQueryParameter("taguser", taguser)
            .appendQueryParameter("userpostimage", images)
            .build().toString();

答案 2 :(得分:0)

检查以下代码并尝试

String encodeEmail = Uri.encode("your.email@add.com");
String encodeDesc = Uri.encode("description is going here");
String url_offerAdd= UrlString.url_string+"/offer_Add.php?email="+encodeEmail +"&desc="+encodeDesc ;