Q:

Android - Encoding arabic text in url doesn't work

So in my fragment I am trying to get json response based on the user's choices from spinner items, and the choices are in Arabic, I tried to decode the strings that I put in the url with URLEncoder.encode(string, "UTF-8").toString() and when I send the url without decoding it just returns a 404 response code from the volley library. 
Here's what my code looks like. I try to encode the strings from the selected spinner items in my getTripPrice() method. I am not sure what's wrong or why it won't work.
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);
    }

}

This is what my JSON data looks like, in case the problem might be with the pasring
{
    "GetTripPriceResult": {
        "TripID": 14,
        "TripName": "التجمع-العاشر",
        "TripFrom": "التجمع",
        "TripTo": "العاشر",
        "DayPrice": 50,
        "MonthPrice": 1500,
        "TripNotes": "",
        "Stations": [
            {
                "StationID": 26,
                "StationName": "شارع التسعين",
                "StationNotes": "Second Note"
            },
            {
                "StationID": 29,
                "StationName": "شارع اخناتون",
                "StationNotes": ""
            }
        ]
    }
}

A:

The issue was that I used URLEncoder.encode(); instead of using Uri.encode(); now it's working as I use Uri.encode(); with all the string that are in Arabic.

