1

したがって、私のフラグメントでは、スピナーアイテムからのユーザーの選択に基づいてjson応答を取得しようとしています。選択はアラビア語で、URLに入力した文字列をデコードしようとしましたが、デコード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データがどのように見えるかです

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

1 に答える 1

0

問題は、アラビア語のすべての文字列で使用するため、現在使用URLEncoder.encode();する代わりに使用したことです。Uri.encode();Uri.encode();

于 2015-08-30T03:23:14.173 に答える