我想使用jsonarrayrequest向api发出请求。我想要获取数据并在适配器上使用它,但由于我什么都没得到,我的适配器在getITemCount中给出了一个错误,因为size方法没有List。我在onCreate of activity中创建了这个片段,我想要获取数据。这个片段有一个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();
}
}
答案 0 :(得分: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;
}
} `
尝试单例的这个实现......我认为实现是个问题。