如何从JSONObject请求

时间:2016-09-02 08:41:31

标签: android json android-volley

如何从ArrayList请求中获取JSONObject

我尝试过界面,但它不起作用。 Setter和Getter也不起作用。我使用的是RecyclerView

这是我的代码,

public class EditDoctor extends Fragment {
    RecyclerView recyclerView;
    String key;
    String url;
    RecyclerAdapterEditDoctor edit;
    SharedPreferences sharedPreferences;
    RequestQueue queue;
    ArrayList<EditDoctorModel> listOfPojo = new ArrayList<>();

    public EditDoctor()
    {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstaneState)
    {
        View view = inflater.inflate(R.layout.editdoctor,container,false);
        Context context = getActivity();
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        key = sharedPreferences.getString("key", "");

        return view;
    }

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        setRetainInstance(true);

    }

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

        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler1);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        queue = Volley.newRequestQueue(getActivity());
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        key  = sharedPreferences.getString("key", "");
        url = "http://192.168.0.153:9091/drrate/api/profile/Search?page=1&size=10&filter=[{\"col\":\"MemberKey\",\"val\":\""+key+"\",\"cond\":\"Equal\"}]";
        getJSONRequest();
        edit = new RecyclerAdapterEditDoctor(getActivity(),listOfPojo);
        recyclerView.setAdapter(edit);

    }

    // This is the Request 
    public void getJSONRequest() {

        JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url,
            new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                ArrayList<EditDoctorModel>temp = new ArrayList<>();
                try {
                    JSONArray Jarray = response.getJSONArray("Data");
                    for (int i = 0; i < Jarray.length(); i++) {
                        JSONObject object = Jarray.getJSONObject(i);
                        EditDoctorModel editDoctorModel = new EditDoctorModel();
                        editDoctorModel.setFullName(object.getString("Name"));
                        editDoctorModel.setProfession(object.getString("Profession"));
                        editDoctorModel.setUrl(object.getString("Image"));
                        temp.add(editDoctorModel);
                    }
                    // this work but android monitor says that skipping layout adapter not attached
                    // edit = new RecyclerAdapterEditDoctor(getActivity(),listOfPojo);
                    // recyclerView.setAdapter(edit);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

           @Override
           public void onErrorResponse(VolleyError error) {

           }
    });
    queue.add(req);
}

请帮帮我。提前谢谢。

2 个答案:

答案 0 :(得分:2)

在代码中制作一些字符

public class EditDoctor extends Fragment {
    RecyclerView recyclerView;
    String key;
    String url;
    RecyclerAdapterEditDoctor edit;
    SharedPreferences sharedPreferences;
    RequestQueue queue;
    ArrayList<EditDoctorModel> listOfPojo;

    public EditDoctor()
    {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstaneState)
    {
        View view = inflater.inflate(R.layout.editdoctor,container,false);
        Context context = getActivity();
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        key = sharedPreferences.getString("key", "");

        return view;
    }

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        setRetainInstance(true);

    }

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

        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler1);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        queue = Volley.newRequestQueue(getActivity());
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        key  = sharedPreferences.getString("key", "");
        url = "http://192.168.0.153:9091/drrate/api/profile/Search?page=1&size=10&filter=[{\"col\":\"MemberKey\",\"val\":\""+key+"\",\"cond\":\"Equal\"}]";
        getJSONRequest();
        edit = new RecyclerAdapterEditDoctor(getActivity(),listOfPojo);
        recyclerView.setAdapter(edit);

    }

    // This is the Request 
    public void getJSONRequest() {

        JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url,
            new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                listOfPojo = new ArrayList<>();
                try {
                    JSONArray Jarray = response.getJSONArray("Data");
                    for (int i = 0; i < Jarray.length(); i++) {
                        JSONObject object = Jarray.getJSONObject(i);
                        EditDoctorModel editDoctorModel = new EditDoctorModel();
                        editDoctorModel.setFullName(object.getString("Name"));
                        editDoctorModel.setProfession(object.getString("Profession"));
                        editDoctorModel.setUrl(object.getString("Image"));
                        listOfPojo.add(editDoctorModel);
                    }
                    // this work but android monitor says that skipping layout adapter not attached
                    // edit = new RecyclerAdapterEditDoctor(getActivity(),listOfPojo);
                    // recyclerView.setAdapter(edit);
            edit.updateRecyler(listOfPojo);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

           @Override
           public void onErrorResponse(VolleyError error) {

           }
    });
    queue.add(req);
}

//在适配器中添加以下方法

public void updateRecyler(ArrayList<EditDoctorModel> list){
this.your_adapterModel_list=list;
notifyDatasetChanged();
}

每当Volley得到回应,你的recyler会自动更新。

答案 1 :(得分:1)

将已解析的数据添加到数据集listOfPojo,并在适配器上调用notifyDataSetChanged

试试这个,

@Override
public void onResponse(JSONObject response) {
    ArrayList<EditDoctorModel> temp = new ArrayList<>();
    try {
        JSONArray Jarray = response.getJSONArray("Data");
        for (int i = 0; i < Jarray.length(); i++) {
            JSONObject object = Jarray.getJSONObject(i);
            EditDoctorModel editDoctorModel = new EditDoctorModel();
            editDoctorModel.setFullName(object.getString("Name"));
            editDoctorModel.setProfession(object.getString("Profession"));
            editDoctorModel.setUrl(object.getString("Image"));
            temp.add(editDoctorModel);
        }

        // add the data in temp to listOfPojo
        listOfPojo.clear();
        listOfPojo.addAll(temp);

        // notify the adapter that the data has changed
        edit.notifyDataSetChanged();

    } catch (JSONException e) {
        e.printStackTrace();
    }
}