/* * * * Copyright 2015 Van Shu * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * */ package com.mobimvp.cliques.ui.fragment; import android.os.Bundle; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.android.volley.NetworkError; import com.android.volley.ParseError; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.ServerError; import com.android.volley.TimeoutError; import com.android.volley.VolleyError; import com.mobimvp.cliques.R; import com.mobimvp.cliques.service.RequestManager; import java.io.UnsupportedEncodingException; import static com.mobimvp.cliques.util.LogUtils.makeLogTag; /** * Created by Van on 2014/11/29. */ public class BaseFragment extends ProgressFragment { private static final String TAG = makeLogTag(BaseFragment.class); public Response.ErrorListener errorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (error instanceof ParseError) { Toast.makeText(getActivity(), "Parsing error " + error.getMessage(), Toast.LENGTH_SHORT).show(); } else if (error instanceof NetworkError) { Toast.makeText(getActivity(), "Check your connection and try again " + error.getMessage(), Toast.LENGTH_SHORT).show(); } else if (error instanceof ServerError) { try { Toast.makeText(getActivity(), "Server error.Response code is : " + error.networkResponse.statusCode + ", : " + new String(error.networkResponse.data, "UTF-8"), Toast.LENGTH_SHORT).show(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else if (error instanceof TimeoutError) { Toast.makeText(getActivity(), "Connection times out and try again", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getActivity(), "Unknown error " + error.getMessage(), Toast.LENGTH_SHORT).show(); } } }; protected void addRequest(Request request) { RequestManager.addRequest(request, this); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root=inflater.inflate(R.layout.fragment_progress_layout,container,false); return root; } @Override public void onStop() { super.onStop(); RequestManager.cancelAll(this); } protected void setEmptyView(){ setContentEmpty(true); setContentShown(true); } protected void showResult(){ setContentEmpty(false); setContentShown(true); } }