2

I Tring to monitor my site performance day by day activity with help of google api

and i tried to fetch items in network request from googleapi of pagespeedonline

but its not working on my code

REST API link :

https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://lifemachi.blogspot.com&key=AIzaSyBfHVlhNEnf26Ea8-ZOhiYOe0HrQZtLvRI&category=performance&strategy=desktop

and i try to get particularly

lighthouseResult -> audits -> network-requests ->details-> items

and store each items into record...

i tried below codes


package FirstTestNgPackage;



import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import org.json.*;

public class testingJSON {
       static String inline = "";
   public static void main(String args[]) throws JSONException, InterruptedException, IOException {
       // url
       URL url = new URL("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://lifemachi.blogspot.com&key=AIzaSyBfHVlhNEnf26Ea8-ZOhiYOe0HrQZtLvRI&category=performance&strategy=desktop");
      // read it from URL
       Scanner sc = new Scanner(url.openStream()); Thread.sleep(300);
       String jsonDataString = sc.nextLine();
       while(sc.hasNext())
        {
            inline+=sc.nextLine();
        }
       sc.close();
        
      List<String> list = new ArrayList<String>();
      
   
// just print that inline var
        
        System.out.println(inline);
        System.out.println("--------1");
    
      }
   }

and i got proper output... but how to store items values in list ?

Thanks in advance

  • Not an answer to your question but hopefully an answer to your actual problem (monitoring site performance) If you are wanting to monitor day to day activity the PSI API is not that useful for you. You may want to check out [this answer I gave](https://stackoverflow.com/questions/64312048/measure-performance-of-web-application-from-mobile/64314433#64314433) on how to measure performance of an application and build your own solution as it will give you much better real world data as PSI data is 28 days rolling for Field Data and Lab Data is not going to fluctuate much unless you change stuff – GrahamTheDev Oct 15 '20 at 12:53

3 Answers3

1

First you must parse the output arrray named "inline" to valid JSON string . Here you can use the functionalities of org.json library,

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import org.json.*;

    public class testingJSON {

        static String inline = "";

        public static void main(String args[]) throws JSONException, InterruptedException, IOException {
     
         // url
         URL url = new URL("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://lifemachi.blogspot.com&key=AIzaSyBfHVlhNEnf26Ea8-ZOhiYOe0HrQZtLvRI&category=performance&strategy=desktop");
    
        // read it from URL

        Scanner sc = new Scanner(url.openStream()); Thread.sleep(300);
        String jsonDataString = sc.nextLine();

            while(sc.hasNext()){
                inline+=sc.nextLine();
            }

            sc.close();

            // just print that inline var
            System.out.println(inline);
            System.out.println("--------1");

            //Tokenize the string data json array
            JSONArray data = new JSONArray(new JSONObject(new JSONTokener(inline)));

            //or JSONArray data = new JSONArray(new JSONObject(inline));

            //The array list we want to insert the formatted JSON string
            ArrayList<String> list = new ArrayList<String>();
    
            if(data !=null){
                for(int i=0;i<data.length();i++){
                    list.add(data.getString(i));
                }
            }

            System.out.println(list);
        }
    }

Here I have got following error

Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 3 [character 4 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:432)
    at org.json.JSONObject.<init>(JSONObject.java:184)
    at testingJson.main(testingJson.java:31)

From this we can identify that something missed-format with inline variable while tokenize that to JSON string

In JSON string the format must be like this [ { the JSON data } ] .

Dharman
  • 30,962
  • 25
  • 85
  • 135
Kaviranga
  • 576
  • 2
  • 10
  • 24
1

If you only want to retrieve the JSON array items in whole JSON response, it can be easily done by using JsonPath as follows:

Code snippet

List<String> items = JsonPath.parse(inline).read("$.lighthouseResult.audits.network-requests.details.items");

Maven dependency

<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->
<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.4.0</version>
</dependency>
LHCHIN
  • 3,679
  • 2
  • 16
  • 34
0

Assuming you have the complete json response in inline variable, you can convert it to JSONObject and then keep on reading the child attributes.

JSONObject jsonObject = new JSONObject(inline);
JSONObject lighthouseResult = jsonObject.getJSONObject("lighthouseResult");
JSONObject audits = lighthouseResult.getJSONObject("audits");
JSONObject networkRequests = audits.getJSONObject("network-requests");
JSONObject details = networkRequests.getJSONObject("details");

//Notice that here we are reading an array
JSONArray items = details.getJSONArray("items");

// Create String list and add elements to it from items JSONArray object
List<String> itemsList = new ArrayList<>();
items.forEach(item -> itemsList.add(item.toString()));
Smile
  • 3,832
  • 3
  • 25
  • 39