使用齐射工具箱的JsonArray请求

时间:2016-11-11 07:33:30

标签: java android json android-volley getjson

我正在尝试在Fragment中实现Recyclerview,但在下载Json Data时我得到的错误是

W/System.err: com.android.volley.ParseError: org.json.JSONException: Value    {"Sales_Report": [{"id":"2","cash":"532","credit":"586","description":"lok","dates":"Nov-09- 2016","camimg_path":"Gallery_images\/Test_gl.jpeg"}, {"id":"3","cash":"214","credit":"980","description":"th","dates":"Nov-09- 2016","camimg_path":"Gallery_images\/Test_gl.jpeg"},{"id":"4","cash":"123","credit":"321","description":"erp","dates":"Nov-09-2016","camimg_path":"Gallery_images\/Test_gl.jpeg"},{"id":"15","cash":"5","credit":"8","description":"5","dates":"Nov-09-2016","camimg_path":"Gallery_images\/galimg2016-11-09T14:44:21.483+05302516.jpeg"},{"id":"14","cash":"2","credit":"3","description":"kk","dates":"Nov-09-2016","camimg_path":"..\/Gallery_images\/galimg2016-11-09T14:42:56.857+05302620.jpeg"}]} of type org.json.JSONObject cannot be  converted to JSONArray
W/System.err:     at   com.android.volley.toolbox.JsonArrayRequest.parseNetworkResponse  (JsonArrayRequest.java:55)
W/System.err:     at com.android.volley.NetworkDispatcher.run (NetworkDispatcher.java:121)
W/System.err: Caused by: org.json.JSONException: Value {"Sales_Report": [{"id":"2","cash":"532","credit":"586","description":"lok","dates":"Nov-09-  2016","camimg_path":"Gallery_images\/Test_gl.jpeg"},{"id":"3","cash":"214","credit":"980","description":"th","dates":"Nov-09-2016","camimg_path":"Gallery_images\/Test_gl.jpeg"},{"id":"4","cash":"123","credit":"321","description":"erp","dates":"Nov-09-2016","camimg_path":"Gallery_images\/Test_gl.jpeg"},{"id":"15","cash":"5","credit":"8","description":"5","dates":"Nov-09-2016","camimg_path":"Gallery_images\/galimg2016-11-09T14:44:21.483+05302516.jpeg"},{"id":"14","cash":"2","credit":"3","description":"kk","dates":"Nov-09-  2016","camimg_path":"..\/Gallery_images\/galimg2016-11-  09T14:42:56.857+05302620.jpeg"}]} of type org.json.JSONObject cannot be    converted to JSONArray
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:96)
W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108) 
W/System.err:     at       com.android.volley.toolbox.JsonArrayRequest.parseNetworkResponse      (JsonArrayRequest.java:50)
W/System.err:   ... 1 more     

这是我的片段从此页面获取错误

public class MessageFragment extends Fragment {
    private List<Content> listSuperHeroes;

    //Creating Views
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.Adapter adapter;

    public MessageFragment() {
        // Required empty public constructor
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_message, container, false);

        recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        //Adding adapter to recyclerview

        listSuperHeroes = new ArrayList<>();

        //Calling method to get data
        getData();
        adapter = new CardAdapter(listSuperHeroes, getActivity());
        //Finally initializing our adapter
        recyclerView.setAdapter(adapter);
        return v;
    }
    private void getData(){
        //Showing a progress dialog
        // final ProgressDialog loading = ProgressDialog.show(getActivity(),"Loading Data", "Please wait...",false,false);

        //Creating a json array request

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL,new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {
                    //Dismissing progress dialog
                    //loading.dismiss();
                    //Log.d(TAG, response.toString());
                    //calling method to parse json array
                    parseData(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();

                }
            });

        //Creating request queue
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());

        //Adding request to the queue
        requestQueue.add(jsonArrayRequest);
    }

    private void parseData(JSONArray array){
        System.out.println("where is my parse");
        for(int i = 0; i<array.length(); i++) {
            Content superHero = new Content();
            JSONObject json = null;
            try {

                json = array.getJSONObject(i);
                superHero.setImageUrl(json.getString(Config.TAG_IMAGE_URL));
                superHero.setName(json.getString(Config.TAG_NAME));
                superHero.setRank(json.getString(Config.TAG_RANK));
                superHero.setRealName(json.getString(Config.TAG_REAL_NAME));
                superHero.setCreatedBy(json.getString(Config.TAG_CREATED_BY));
                superHero.setFirstAppearance(json.getString    (Config.TAG_FIRST_APPEARANCE));
                superHero.setPowers(json.getString(Config.TAG_POWERS));
//              ArrayList<String> powers = new ArrayList<String>();
// 
//              JSONArray jsonArray = json.getJSONArray (Config.TAG_POWERS);
//
//              for(int j = 0; j<jsonArray.length(); j++){
//                   powers.add(((String) jsonArray.get(j))+"\n");
//              }
//              superHero.setPowers(powers);


            } catch (JSONException e) {
                e.printStackTrace();
            }
            listSuperHeroes.add(superHero);
        }

        adapter .notifyDataSetChanged();
    }
}

我正在使用jsonArray请求下载json数据但它没有工作

请帮我解决此错误...提前致谢

1 个答案:

答案 0 :(得分:0)

此代码相当不言自明。阅读文档:

https://developer.android.com/training/volley/request.html

https://developer.android.com/reference/org/json/JSONArray.html

https://developer.android.com/reference/org/json/JSONObject.html

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

    @Override
    public void onResponse(JSONObject response) {
        JSONArray arr = response.getJSONArray("Sales_Report");
        // now you have the array. in this array you have the jsonObjects
        // iterate on this array using a for loop and parse like you did before
    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO Auto-generated method stub

    }
});