0

我想使用 jsonarrayrequest 向 api 发出请求。然后我想获取数据并在适配器上使用它,但由于我什么也没得到,我的适配器在 getITemCount 中给了我一个错误,因为 size 方法没有 List。我在活动的 onCreate 中执行此操作,这是一个片段,我想在其中获取数据。该片段有一个 recycleView 并用于页面查看器。

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SingletonDicas.getInstance(getContext()).setDicasListener(this);
    SingletonDicas.getInstance(getContext()).getAllDicasAPI(getActivity(), DicaJsonParser.isConnectionInternet(getActivity()), "Receitas");

}

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_dicas, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    recyclerView = view.findViewById(R.id.recycleViewDicas);
    recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));

    adapter_dicas = new Adapter_Dicas(dicasListAlimentacao);
    recyclerView.addItemDecoration(new MyDividerItemDecoration(recyclerView.getContext(), LinearLayoutManager.VERTICAL, 16));
    recyclerView.setAdapter(adapter_dicas);
}

    }

这是我的 getAllDicas 方法和我的 setDicasListener,在单例中。我找不到原因,但在第一种方法中,它只会持续到第一个 System.out.println,它永远不会通过实际请求,只是“跳出”。

public static synchronized SingletonDicas getInstance(Context context) {
    if(INSTANCE == null){

        INSTANCE =  new SingletonDicas(context);
        volleyQueue = Volley.newRequestQueue(context);
    }
    return INSTANCE;
}

public void getAllDicasAPI(final Context context, boolean isConnected, final String type){

    System.out.println("--> isconnected : " + isConnected);

    JsonArrayRequest req = new JsonArrayRequest (Request.Method.GET, mUrlAPIDicas, null, new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    dicas = DicaJsonParser.parserJsonDicas(response, context, type);

                    adicionarDicasBD(dicas);

                    if(dicasListener != null)
                    {
                        dicasListener.onRefreshListaDicas(dicas);
                    }
                }

            }, new Response.ErrorListener(){
                public void onErrorResponse(VolleyError error){

                    System.out.println("--> Erro: ");

                }
            });
    volleyQueue.add(req);
}

public void setDicasListener(DicasListener dicasListener)
{
    this.dicasListener = dicasListener;
}

我的 DicasJsonParser:

public class DicaJsonParser {

    public static ArrayList<Dica> parserJsonDicas(JSONArray response, Context contexto, String type) {
        System.out.println("--> PARSER LISTA DICAS: " + response.toString());
        ArrayList<Dica> tempListaDicas = new ArrayList<Dica>();
        try {
            for (int i = 0; i < response.length(); i++)
            {
                JSONObject dica = (JSONObject) response.get(i);

                int idDica = dica.getInt("Id");
                String channelDica = dica.getString("Channel");
                String tituloDica = dica.getString("Title");
                String conteudoDica = dica.getString("Content");

                if(dica.getString("Channel").equals(type))
                {
                    Dica auxDica = new Dica(idDica,channelDica,tituloDica,conteudoDica);
                    tempListaDicas.add(auxDica);
                }

            }
        }
        catch (JSONException e)
        {
            e.printStackTrace();
            Toast.makeText(contexto, "Erro", Toast.LENGTH_SHORT).show();
        }
        return tempListaDicas;
    }

    public static boolean isConnectionInternet(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();

        return networkInfo != null && networkInfo.isConnectedOrConnecting();
    }

}
4

1 回答 1

1

` 公共类 SingletonDicas {

private static SingletonDicas singletonDicas;
private RequestQueue requestQueue;

private SingletonDicas(Context applicationContext) {
    this.requestQueue = Volley.newRequestQueue(applicationContext);
}

public static SingletonDicas getInstance(Context applicationContext) {
    if (singletonDicas == null) {
        synchronized (SingletonDicas.class) {
            if (singletonDicas == null) {
                singletonDicas = new SingletonDicas(applicationContext);
            }
        }
    }
    return singletonDicas;
}

} `

试试这个单例实现...我认为实现是有问题的。

于 2017-12-23T11:50:07.883 回答