Android - 在 url 中编码阿拉伯文本不起作用

Android - Encoding arabic text in url doesn't work

所以在我的片段中,我试图根据用户从微调项中的选择获得 json 响应,并且选择是阿拉伯语,我试图解码我放入 url 和 URLEncoder.encode(string, "UTF-8").toString(),当我发送 url 而不解码它时,只是 returns 来自 volley 库的 404 响应代码。

我的代码如下所示。我尝试在我的 getTripPrice() 方法中对来自所选微调器项目的字符串进行编码。我不确定哪里出了问题或为什么它不起作用。

public class HomeFragment extends Fragment {

    public static final String TAG_STRING_URL = "url";
    public String TAG_TRIP_PRICE = "url";
    public static final String TAG_CITY_NAME = "CityName";
    public static final String TAG_CITY_ID = "CityID";
    public static final String TAG_ARRAY_NAME = "GetCitiesResult";
    public static String TAG = HomeFragment.class.getSimpleName();
    public String tripFromSelected, tripToSelected;
    public Spinner spinnerFrom, spinnerTo;
    public TextView textView;
    public String jsonResponse, tripPriceString;
    Button button;
    public Context globalContext = null;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    private OnFragmentInteractionListener mListener;


    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public HomeFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        getFromCities();
        getToCities();
    }


    public class FromCity {
        private String id;
        private String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
    public class ToCity {
        private String id;
        private String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
    public ArrayList<String> getFromCitiesList(String string) {
        return null;
    }
    public ArrayList<String> getToCitiesList(String string) {
        return null;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        Button logout = (Button) view.findViewById(R.id.bookNowHome);
        logout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
//                Intent intent = new Intent(getActivity().getApplicationContext(), TripsActivity.class);
//                startActivity(intent);
                getTripPrice();
            }
        });
        textView = (TextView) view.findViewById(R.id.tripPriceText);
        return view;
    }

    public void getFromCities() {
        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, TAG_STRING_URL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                Log.d(TAG, response.toString());
                ArrayList<FromCity> cities = new ArrayList<FromCity>();
                ArrayList<String> cityNames = new ArrayList<String>();

                try {
                    JSONArray jsonArray = response.getJSONArray(TAG_ARRAY_NAME);
                    for (int i = 0; i < jsonArray.length(); i++) {

                        JSONObject jSONObject = jsonArray.getJSONObject(i);
                        String cityID = jSONObject.getString(TAG_CITY_ID);
                        String cityName = jSONObject.getString(TAG_CITY_NAME);

                        FromCity fromCity = new FromCity();
                        fromCity.setName(jSONObject.optString(TAG_CITY_NAME));
                        cities.add(fromCity);
                        cityNames.add(jSONObject.optString(TAG_CITY_NAME));

                        spinnerFrom = (Spinner) getView().findViewById(R.id.tripFromSpinner);
                        spinnerFrom.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, cityNames));
                        spinnerFrom.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                tripFromSelected = spinnerFrom.getSelectedItem().toString();
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getActivity().getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());

            }
        });
        AppController.getInstance(getActivity().getApplicationContext()).addToRequestQueue(jsObjRequest);
    }

    public void getToCities() {
        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, TAG_STRING_URL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());
                ArrayList<ToCity> cities = new ArrayList<ToCity>();
                ArrayList<String> cityNames = new ArrayList<String>();

                try {
                    JSONArray jsonArray = response.getJSONArray(TAG_ARRAY_NAME);
                    for (int i = 0; i < jsonArray.length(); i++) {

                        JSONObject jSONObject = jsonArray.getJSONObject(i);
                        String cityID = jSONObject.getString(TAG_CITY_ID);
                        String cityName = jSONObject.getString(TAG_CITY_NAME);

                        ToCity toCity = new ToCity();
                        toCity.setName(jSONObject.optString(TAG_CITY_NAME));
                        cities.add(toCity);
                        cityNames.add(jSONObject.optString(TAG_CITY_NAME));

                        spinnerTo = (Spinner) getView().findViewById(R.id.tripToSpinner);
                        spinnerTo.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, cityNames));
                        spinnerTo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                tripToSelected = spinnerTo.getSelectedItem().toString();
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getActivity().getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
            }
        });
        AppController.getInstance(getActivity().getApplicationContext()).addToRequestQueue(jsObjRequest);
    }

    public void getTripPrice()  {
        JsonObjectRequest jsonObjReq = null;
        try {
            jsonObjReq = new JsonObjectRequest(Request.Method.GET, TAG_TRIP_PRICE + URLEncoder.encode(tripFromSelected, "utf-8").toString() + "/" + URLEncoder.encode(tripToSelected, "utf-8").toString() , null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    VolleyLog.d("Response: ", response.toString());
                    try {

                        JSONObject getPriceResult = response.getJSONObject("GetTripPriceResult");
                        tripPriceString = getPriceResult.getString("MonthPrice");
                        textView.setText(tripPriceString);

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(globalContext, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        AppController.getInstance(globalContext).addToRequestQueue(jsonObjReq);
    }


    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction(Uri uri);
    }

}

这是我的 JSON 数据的样子,以防问题出在 pasring

{
    "GetTripPriceResult": {
        "TripID": 14,
        "TripName": "التجمع-العاشر",
        "TripFrom": "التجمع",
        "TripTo": "العاشر",
        "DayPrice": 50,
        "MonthPrice": 1500,
        "TripNotes": "",
        "Stations": [
            {
                "StationID": 26,
                "StationName": "شارع التسعين",
                "StationNotes": "Second Note"
            },
            {
                "StationID": 29,
                "StationName": "شارع اخناتون",
                "StationNotes": ""
            }
        ]
    }
}

问题是我使用 URLEncoder.encode(); 而不是使用 Uri.encode(); 现在它正在工作,因为我对所有阿拉伯语字符串使用 Uri.encode();