Okhttp解析和recyclerview片段?

时间:2019-05-18 09:12:14

标签: okhttp3

我在使用Okhttp中的list_state时遇到问题,当我尝试在适配器中传递它时,结果为空。 我不明白,因为在onResponse内部使用的所有变量都不会传递到其外部。 我试图在onResponse内设置一个recyclerview和适配器,但是在它上面出现了错误并导致ap崩溃。 我使用的代码在下面有没有人可以帮助我? 对不起,我的英语。

public class dashboard_device extends Fragment implements ListOwner{
    RecyclerView mRecicleView;
    RecyclerView.LayoutManager mLayoutManager;
    RecyclerView.Adapter mAdapter;
    ArrayList<String> lista_show;
    ArrayList<String> lista_state = new ArrayList<String> ();
    ArrayList<String> lista_prestate = new ArrayList<String> ();
    String p,d;
    String myResponse = null;
    List<risposta_json> posts;
    public dashboard_device() {
        // Required empty public constructor

    }


    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        View fragmentView =  inflater.inflate (R.layout.dashboard_device_layout, container, false);
        final ArrayList<String> lista_show = new ArrayList<String> ();

        dashboard_DB db1 = new dashboard_DB (getContext ());
        //recupero datbase DB
        db1.open();
        Cursor c = db1.ottieniTuttidati();
        if (c.moveToFirst()) {
            do {
                lista_show.add (c.getString(2));
            } while (c.moveToNext());

        }
        db1.close();

        //fine recupero dati da db
        // Inflate the layout for this fragment



        GestioneDB db = new GestioneDB(getContext ());
        db.open();
        Cursor c1 = db.ottieniTuttidati();
        if (c1.moveToFirst()) {
            do {

                p =  c1.getString(1);
                d = c1.getString(2);

            } while (c1.moveToNext());
        }
        db.close();
        //fine recupero dati da db
        final String url = p;
        String token=d;

        OkHttpClient client = new OkHttpClient ();
        Request request = new Request.Builder()
                .url("https://"+url+"/api/states")
                .addHeader("Authorization", "Bearer " + token)
                .build();

        client.newCall(request).enqueue(new Callback () {
            @Override
            public void onFailure(Call call, IOException e) {e.printStackTrace();}

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if (response.isSuccessful()) {
                    myResponse = response.body().string();
                    GsonBuilder gsonb = new GsonBuilder();
                    Gson gson = gsonb.create();
                    String jsonOutput = myResponse;
                    Type listType = new TypeToken<List<risposta_json>> (){}.getType();
                    posts = gson.fromJson(jsonOutput, listType);
                   // Log.d("MYRESPONSE", String.valueOf (myResponse));
                    //attributes = gson.fromJson (myResponse, risposta_json.class);
                        for (int i = 0; i<lista_show.size (); i++) {
                            for (int l = 0; l<posts.size ();l++) {
                             if (posts.get (l).getAttributes ().getFriendly_name ()!=null && posts.get (l).getAttributes ().getFriendly_name ().equalsIgnoreCase (lista_show.get (i))) {
                              lista_state.add (posts.get (l).getState ());

                             }
                        }

                    }

                }

            }

        });
        Log.d("STATI", String.valueOf (lista_state));

        mRecicleView = container.findViewById (R.id.Reclycler_View);
        mRecicleView.setHasFixedSize (true);
        mLayoutManager = new GridLayoutManager (getContext (),3);
        mAdapter = new adapter_dash (lista_show,lista_state, getContext (),dashboard_device.this);
        mRecicleView.setLayoutManager (mLayoutManager);
        mRecicleView.setAdapter (mAdapter);
        runanimation1(mRecicleView,0);
        Log.d("LISTA SHOW", String.valueOf (lista_show));
        return fragmentView;

    }

    private void runanimation1(RecyclerView mRecicleView, int type) {
        Context context=mRecicleView.getContext ();
        LayoutAnimationController controller = null;
        if(type==0)
            controller = AnimationUtils.loadLayoutAnimation (context,R.anim.layout_animation);

        mRecicleView.setLayoutAnimation (controller);
        mRecicleView.getAdapter ().notifyDataSetChanged ();
        mRecicleView.scheduleLayoutAnimation ();
    }

    @Override
    public void push(ArrayList<String> list) {

    }

}

1 个答案:

答案 0 :(得分:0)

已解决 .getactivity()。runOnUiThread(新的Runnable)等...等等...