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

时间:2015-08-20 20:27:45

标签: android json url-encoding arabic-support

所以在我的片段中,我试图根据用户对微调器项目的选择来获取json响应,并且选择是阿拉伯语,我试图解码我在URLEncoder.encode(string, "UTF-8").toString()的URL中放入的字符串以及何时我发送网址而不解码它只是从排球库返回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": ""
            }
        ]
    }
}

1 个答案:

答案 0 :(得分:0)

问题是我使用了URLEncoder.encode();而不是使用Uri.encode();现在它正在使用Uri.encode();,而所有字符串都是阿拉伯语。