简体   繁体   中英

how can i replace unicode String in url for getting json response?

I have a this api from aparat site . I use this method for search

http://www.aparat.com/etc/api/videoBySearch/text/[نوروز]

I should fill last parameter with my editText value . For getting json . If i send english string it worked . But if i send unicode string like persian , It can't work and when i logging that , it say

java.lang.RuntimeException: Bad URL null

This is my JsonResponse method :

   //this method call when search button pressed !
    private void sendJsonRequest() {
        String rawQuery = edtsearchQuery.getText().toString();
       ==> String first_url =   "http://www.aparat.com/etc/api/videoBySearch/text/"+ rawQuery;



        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, first_url, (String) null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                parseJsonResponse(response);
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("onErrorResponseSenJsReq" , error.getMessage());
            }
        });
        AppController.getInstance().addToRequestQueue(request);
    }

Instead of sending نوروز in URL use URLEncoder for encoding it before appending it in main URL:

String strSearchQuery= URLEncoder.encode(rawQuery, "utf-8");
String first_url="http://www.aparat.com/etc/api/videoBySearch/text/"
                                                        +strSearchQuery;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM