З / Залп: [1] 5.onErrorResponse: Продукт - PullRequest
0 голосов
/ 10 ноября 2018

Как вставить данные из MySQL в Gridview для фрагментов? Я пробовал это с помощью метода кодирования, используемого для деятельности. Я сильно изменился, но это все еще не так. Это моя кодировка:

/**
 * A simple {@link Fragment} subclass.
 */
public class Product extends Fragment implements 
SwipeRefreshLayout.OnRefreshListener {

GridView grid_product;
SwipeRefreshLayout swipe;

List<ProductData> newsList = new ArrayList<ProductData>();

private static final String TAG = Product.class.getSimpleName();

private static String url_list   = Server.URL + "news.php?offset=";

private int offSet = 0;

int no;

ProductAdapter adapter;

public static final String TAG_NO       = "no";
public static final String TAG_ID       = "id";
public static final String TAG_JUDUL    = "judul";
public static final String TAG_TGL      = "tgl";
public static final String TAG_ISI      = "isi";
public static final String TAG_GAMBAR   = "gambar";

Handler handler;
Runnable runnable;


public Product() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_product, container, false);
    swipe = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
    grid_product = (GridView) view.findViewById(R.id.grid_product);
    setProduct();

    return view;
}

private void setProduct(){


    adapter = new ProductAdapter(getActivity(), newsList);
    grid_product.setAdapter(adapter);
    grid_product.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //pindah activity
            startActivity(new Intent(
                    getActivity(), DetailActivity.class
            ));

        }
    });

Я не знаю, где ошибка:

    swipe.setOnRefreshListener(this);

    swipe.post(new Runnable() {
                   @Override
                   public void run() {
                       swipe.setRefreshing(true);
                       newsList.clear();
                       adapter.notifyDataSetChanged();
                       callNews(0);
                   }
               }
    );

    grid_product.setOnScrollListener(new AbsListView.OnScrollListener() {

        private int currentVisibleItemCount;
        private int currentScrollState;
        private int currentFirstVisibleItem;
        private int totalItem;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            this.currentScrollState = scrollState;
            this.isScrollCompleted();
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            this.currentFirstVisibleItem = firstVisibleItem;
            this.currentVisibleItemCount = visibleItemCount;
            this.totalItem = totalItemCount;
        }

        private void isScrollCompleted() {
            if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
                    && this.currentScrollState == SCROLL_STATE_IDLE) {

                swipe.setRefreshing(true);
                handler = new Handler();

                runnable = new Runnable() {
                    public void run() {
                        callNews(offSet);
                    }
                };

                handler.postDelayed(runnable, 3000);
            }
        }

    });

}

@Override
public void onRefresh() {
    newsList.clear();
    adapter.notifyDataSetChanged();
    callNews(0);
}

Я не знаю, где ошибка:

private void callNews(int page){

    swipe.setRefreshing(true);

    // Creating volley request obj
    JsonArrayRequest arrReq = new JsonArrayRequest(url_list + page,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());

                    if (response.length() > 0) {
                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                ProductData news = new ProductData();

                                no = obj.getInt(TAG_NO);

                                news.addId(obj.getString(TAG_ID));
                                news.addJudul(obj.getString(TAG_JUDUL));

                                if (obj.getString(TAG_GAMBAR) != "") {
                                    news.addGambar(obj.getString(TAG_GAMBAR));
                                }

                                news.addDatetime(obj.getString(TAG_TGL));
                                news.addIsi(obj.getString(TAG_ISI));

                                // adding news to news array
                                newsList.add(news);

                                if (no > offSet)
                                    offSet = no;

                                Log.d(TAG, "offSet " + offSet);

                            } catch (JSONException e) {
                                Log.e(TAG, "JSON Parsing error: " + e.getMessage());
                            }

                            // notifying list adapter about data changes
                            // so that it renders the list view with updated data
                            adapter.notifyDataSetChanged();
                        }
                    }
                    swipe.setRefreshing(false);
                }

            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            swipe.setRefreshing(false);
        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(arrReq);
}

}

...