как получить объект с залпом - PullRequest
0 голосов
/ 16 июня 2019

Я пытаюсь получить данные с сервера, но не могу получить

{
    "call_duration_category": {
        "0": 0,
        "11-30": 0,
        "0-10": 0,
        "61-180": 0,
        "31-60": 0,
        "180+": 0
    },
    "user_call_data": [
        {
            "user_id": 17,
            "prospect_number": "+919958561247",
            "call_type": "out_going",
            "talk_time": 30,
            "start_datetime": "1970-01-01T05:30:00Z",
            "end_datetime": "2019-06-16T09:13:50Z",
            "creation_datetime": "2019-06-16T03:43:50.455108Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+919718118877",
            "call_type": "incoming",
            "talk_time": 30,
            "start_datetime": "2019-06-16T09:48:46Z",
            "end_datetime": "2019-06-16T09:50:15Z",
            "creation_datetime": "2019-06-16T04:20:16.527227Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+919582647324",
            "call_type": "incoming",
            "talk_time": 30,
            "start_datetime": "2019-06-16T10:02:25Z",
            "end_datetime": "2019-06-16T10:03:34Z",
            "creation_datetime": "2019-06-16T04:33:35.313637Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+917428268915",
            "call_type": "out_going",
            "talk_time": 30,
            "start_datetime": "1970-01-01T05:30:00Z",
            "end_datetime": "2019-06-16T10:12:33Z",
            "creation_datetime": "2019-06-16T04:42:34.022740Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+919958561247",
            "call_type": "incoming",
            "talk_time": 30,
            "start_datetime": "2019-06-16T10:14:39Z",
            "end_datetime": "2019-06-16T10:15:03Z",
            "creation_datetime": "2019-06-16T04:45:03.013636Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+917428268915",
            "call_type": "incoming",
            "talk_time": 30,
            "start_datetime": "2019-06-16T10:28:19Z",
            "end_datetime": "2019-06-16T10:30:26Z",
            "creation_datetime": "2019-06-16T05:00:26.615132Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+919582647324",
            "call_type": "incoming",
            "talk_time": 30,
            "start_datetime": "2019-06-16T11:13:27Z",
            "end_datetime": "2019-06-16T11:13:44Z",
            "creation_datetime": "2019-06-16T05:43:44.646805Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+919990925305",
            "call_type": "out_going",
            "talk_time": 30,
            "start_datetime": "2019-06-16T11:13:27Z",
            "end_datetime": "2019-06-16T11:14:10Z",
            "creation_datetime": "2019-06-16T05:44:10.243103Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+919582647324",
            "call_type": "out_going",
            "talk_time": 30,
            "start_datetime": "2019-06-16T11:13:27Z",
            "end_datetime": "2019-06-16T11:14:36Z",
            "creation_datetime": "2019-06-16T05:44:36.707062Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+917428268915",
            "call_type": "out_going",
            "talk_time": 30,
            "start_datetime": "2019-06-16T11:13:27Z",
            "end_datetime": "2019-06-16T11:15:34Z",
            "creation_datetime": "2019-06-16T05:45:33.999173Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+917428268915",
            "call_type": "incoming",
            "talk_time": 30,
            "start_datetime": "2019-06-16T11:20:30Z",
            "end_datetime": "2019-06-16T11:21:28Z",
            "creation_datetime": "2019-06-16T05:51:29.083218Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+917210700848",
            "call_type": "out_going",
            "talk_time": 30,
            "start_datetime": "2019-06-16T11:20:30Z",
            "end_datetime": "2019-06-16T11:22:05Z",
            "creation_datetime": "2019-06-16T05:52:05.179083Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+919582647324",
            "call_type": "out_going",
            "talk_time": 30,
            "start_datetime": "2019-06-16T11:20:30Z",
            "end_datetime": "2019-06-16T11:24:46Z",
            "creation_datetime": "2019-06-16T05:54:46.443063Z"
        },
        {
            "user_id": 17,
            "prospect_number": "+917428268915",
            "call_type": "missed_call",
            "talk_time": 45,
            "start_datetime": "1970-01-01T05:30:00Z",
            "end_datetime": "2019-06-16T18:54:47Z",
            "creation_datetime": "2019-06-16T13:24:47.687128Z"
        }
    ],
    "total_call_data": [
        {
            "total_duration": 30,
            "user_id": 17,
            "call_type": "out_going",
            "hour": 3,
            "total_calls": 1
        },
        {
            "total_duration": 90,
            "user_id": 17,
            "call_type": "incoming",
            "hour": 4,
            "total_calls": 3
        },
        {
            "total_duration": 30,
            "user_id": 17,
            "call_type": "out_going",
            "hour": 4,
            "total_calls": 1
        },
        {
            "total_duration": 90,
            "user_id": 17,
            "call_type": "incoming",
            "hour": 5,
            "total_calls": 3
        },
        {
            "total_duration": 150,
            "user_id": 17,
            "call_type": "out_going",
            "hour": 5,
            "total_calls": 5
        },
        {
            "total_duration": 45,
            "user_id": 17,
            "call_type": "missed_call",
            "hour": 13,
            "total_calls": 1
        }
    ]
}

Я хочу получить все запросы от этого JSON с залпом

Ответы [ 2 ]

0 голосов
/ 01 июля 2019
String JSON_URL ="https://www.server_reponse.com"

StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                   //getting the whole json object from the response
                    JSONObject obj = new JSONObject(response); 

                    // Here you can get the call_duration_category json object
                    JSONObject call_duration_category_obj = json.getJSONObject("call_duration_category");

                    // Now we need process the remaining data -> user_call_data it's json array so we need get those object inside array and iterate those items

                    //so here we are getting that json array 
                    JSONArray user_call_data_arr = 
                                             obj.getJSONArray("user_call_data");

                  for (int i = 0; i < user_call_data_arr.length(); i++) {
              //getting the json object of the particular index inside the array
                        JSONObject user_call_obj = user_call_data_arr .getJSONObject(i);

                     String user_id = user_call_obj.getString("user_id");
                    }


                            }catch (JSONException e) {
                             e.printStackTrace();
                           }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //displaying the error in toast if occurrs
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

        //creating a request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //adding the string request to request queue
        requestQueue.add(stringRequest);
0 голосов
/ 16 июня 2019

Вы можете сделать это довольно легко с Gson и вот хороший учебник youtube

Вот небольшой пример кода

    public void getFromServer(){
    final String url =YOUR URL HERE
    final ArrayList<ElementEntity> arrayOfCities = new ArrayList<>();

    // prepare the Request
    final JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET,url,null,
            new Response.Listener<JSONArray>()
            {
                @Override
                public void onResponse(JSONArray response) {
                    // display response
                    Log.d("Response", response.toString());

                    for(int i = 0; i< response.length();i++){
                        try {
                            JSONObject jresponse = response.getJSONObject(i);
                            YourObject yourObject = gson.fromJson(jresponse.toString(),YourObject .class);
                            arrayOfYourObject.add(yourObject);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error.Response", "error");
                }
            });

    // add it to the RequestQueue
    queue.add(getRequest);
}
...