Back to project page SeeKampf.
The source code is released under:
GNU General Public License
If you think the Android project SeeKampf listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package net.avedo.seekampf.fragments; /*from w w w .ja va 2 s .c o m*/ import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import net.avedo.seekampf.R; import net.avedo.seekampf.core.OceanView; import net.avedo.seekampf.core.VolleyActivity; import net.avedo.seekampf.models.Island; import net.avedo.seekampf.utils.AuthGsonRequest; import net.avedo.seekampf.utils.Constants; import net.avedo.seekampf.utils.VolleyErrorHelper; import android.app.Fragment; import android.content.SharedPreferences; import android.content.res.Resources; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; public class OceanFragment extends Fragment { public static final String SERVICE_TAG = "OceanFragment"; private SharedPreferences settings; private Resources res; private AuthGsonRequest<Island[]> request; private boolean isFetching = false; private int retryCount = 0; private OceanView oceanView; @Override public void onActivityCreated(Bundle paramBundle) { // Fetch the application preferences handle ... this.settings = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); // ... and the application resources. this.res = getResources(); super.onActivityCreated(paramBundle); } @Override public void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); } @Override public void onStart() { super.onStart(); fetchData(); } @Override public void onDestroyView() { VolleyActivity.getInstance().cancelPendingRequests(SERVICE_TAG); isFetching = false; super.onDestroyView(); } protected void fetchData() { // Break if already fetching data or there is nothing left to load. if (isFetching) { return; } // Start fetching data ... isFetching = true; // ... and generate the service url. String serviceURL = fetchServiceUrl(); // Setup the request object. request = new AuthGsonRequest<Island[]>(Request.Method.GET, serviceURL, Island[].class, null, new Response.Listener<Island[]>() { @Override public void onResponse(Island[] islands) { if (OceanFragment.this.getView() != null && islands != null) { // Reset the retry counter, ... retryCount = 0; // ... assign the island to the ocean view ... OceanFragment.this.oceanView.setIslands(islands); // ... and reset fetching status. isFetching = false; } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { retryCount++; if (retryCount < Constants.MAXIMUM_RETRIES) { VolleyActivity.getInstance().addToRequestQueue(request, SERVICE_TAG); } Toast.makeText(getActivity(), VolleyErrorHelper.getMessage(error, getActivity()), Toast.LENGTH_LONG).show(); } }); VolleyActivity.getInstance().addToRequestQueue(request, SERVICE_TAG); } protected String fetchServiceUrl() { return Constants.API_URL + "?server=" + settings.getString(res.getString(R.string.prefs_server_key), "1") + "&typ=inseln"; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Fetch the main View, ... View view = inflater.inflate(R.layout.ocean_fragment, container, false); // ... extract the OceanView ... this.oceanView = ((OceanView) view.findViewById(R.id.oceanView)); // ... and return the main view. return view; } }