如何在url中替换unicode String以获取json响应?

时间:2021-01-17 16:01:47

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

我有一个来自aparat网站的api。我用这种方法进行搜索

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

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

我应该用editText值填充最后一个参数。为了获得json。如果我发送英文字符串就可以了。但是,如果我发送像波斯语这样的unicode字符串,它就无法工作,当我记录它时,它说

java.lang.RuntimeException: Bad URL null

java.lang.RuntimeException:错误的URL null

This is my JsonResponse method :

这是我的JsonResponse方法:

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

1 个解决方案

#1


3  

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

不是在URL中发送نوروز而是使用URLEncoder对其进行编码,然后将其附加到主URL中:

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

#1


3  

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

不是在URL中发送نوروز而是使用URLEncoder对其进行编码,然后将其附加到主URL中:

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