Created
August 7, 2015 07:29
-
-
Save eneim/77adf89771a70c441d84 to your computer and use it in GitHub Desktop.
Sample code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class HomeFragment extends Fragment { | |
| private static final String ARG_PARAM1 = "param1"; | |
| private static final String ARG_PARAM2 = "param2"; | |
| private String mParam1; | |
| private String mParam2; | |
| private OnFragmentInteractionListener mListener; | |
| private String urlCitiesObj = "url"; | |
| private static String TAG = HomeFragment.class.getSimpleName(); | |
| private TextView txtResponse; | |
| private String jsonResponse; | |
| // private Context globalContext = null; | |
| // private Context globalContext = getActivity().getApplicationContext(); // don't need this. | |
| 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() { | |
| // Required empty public constructor | |
| } | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| } | |
| // TODO: Rename method, update argument and hook method into UI event | |
| public void onButtonPressed(Uri uri) { | |
| if (mListener != null) { | |
| mListener.onFragmentInteraction(uri); | |
| } | |
| } | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
| View layout = inflater.inflate(R.layout.fragment_home, container, false); | |
| // makeJsonArrayRequest(); // NOTE: don't call any request before your UI is fully initialized | |
| txtResponse = (TextView) layout.findViewById(R.id.txtResponse); | |
| return layout; | |
| } | |
| private Activity mActivity; | |
| @Override | |
| public void onAttach(Activity activity) { | |
| if (activity instanceOf OnFragmentInteractionListener) | |
| mListener = (OnFragmentInteractionListener) activity; // this is how your get your callback | |
| } | |
| @Override | |
| public void onActivityCreated(Bundle something) { | |
| super.onActivityCreated(something); | |
| // call your request here | |
| makeJsonArrayRequest(); | |
| } | |
| private void makeJsonArrayRequest() { | |
| JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, | |
| urlCitiesObj, null, new Response.Listener<JSONObject>() { | |
| @Override | |
| public void onResponse(JSONObject response) { | |
| Log.d(TAG, response.toString()); | |
| try { | |
| JSONArray jsonArray = response.getJSONArray("GetCitiesResult"); | |
| for (int i = 0; i < jsonArray .length(); i++) { | |
| JSONObject jSONObject = jsonArray .getJSONObject(i); | |
| String cityID = jSONObject .getString("CityID"); | |
| String cityName = jSONObject .getString("CityName"); | |
| jsonResponse += "CityID: " + cityID + "\n\n"; | |
| jsonResponse += "CityName: " + cityName + "\n\n"; | |
| txtResponse.setText(jsonResponse); | |
| } | |
| // this make no sense | |
| // if( isAdded()){ | |
| // getActivity().getApplicationContext(); | |
| // } | |
| } catch (JSONException e) { | |
| e.printStackTrace(); | |
| if (isAdded()) { | |
| getActivity().runOnUIThread(new Runnable() { | |
| @Override | |
| public void run() { | |
| Toast.makeText(getActivity(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }, new Response.ErrorListener() { | |
| @Override | |
| public void onErrorResponse(VolleyError error) { | |
| VolleyLog.d(TAG, "Error: " + error.getMessage()); | |
| if (isAdded()) { | |
| getActivity().runOnUIThread(new Runnable() { | |
| @Override | |
| public void run() { | |
| Toast.makeText(getActivity(), "Error: " + error.getMessage(), Toast.LENGTH_LONG).show(); | |
| } | |
| } | |
| } | |
| } | |
| }); | |
| // Adding request to request queue | |
| AppController.getInstance().addToRequestQueue(jsonObjReq); | |
| } | |
| @Override | |
| public void onDetach() { | |
| super.onDetach(); | |
| mListener = null; | |
| } | |
| public interface OnFragmentInteractionListener { | |
| // TODO: Update argument type and name | |
| public void onFragmentInteraction(Uri uri); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment