无法从JsonArray Request获取请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法从JsonArray Request获取请求相关的知识,希望对你有一定的参考价值。

我想使用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;
}

我的Dicas JsonParser:

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();
    }

}
答案

`public class 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;
}

} `

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

以上是关于无法从JsonArray Request获取请求的主要内容,如果未能解决你的问题,请参考以下文章

如何从 JsonArray 中获取 NSDictonarys?

JSONArray 无法根据 indexPath 获取数组

如何解决 JSONArray 问题,无法获取数据,Android Studio

java.lang.String 无法从 Okhttp 转换为 JSONArray

无法从 Django request.POST 获取 POST 数据

Swift - 无法获取 Alamofire.request