0

This json occurs when a destination or origin is outside of the uk, therefore not giving me any results! I need to check for this for a null check so i dont receive null pointer exception

JSON RESULT:

{
"destination_addresses" : [ "Durham, NC, USA" ],
"origin_addresses" : [ "Lancashire, UK" ],
"rows" : [
  {
     "elements" : [
        {
           "status" : "ZERO_RESULTS"
        }
     ]
  }
],

"status" : "OK" }

Code:

public static void extractJsonFromRequest(GoogleResponsePojo response) {
    String destinationAddress = 
    response.getDestination_addresses().get(0);
    String timeTaken = 
response.getRows().get(0).getElements().get(0).getDuration().getText();
    String originAddress = response.getOrigin_addresses().get(0);

    System.out.println("It will take ** "+  timeTaken + " ** to walk 
from " + originAddress + " to " + destinationAddress);
}

Google Reponse POJO which is structure of json:

public class GoogleResponsePojo {

private List<String> destination_addresses;
private List<String> origin_addresses;
private List<Rows> rows;

public List<String> getDestination_addresses() {
    return destination_addresses;
}

public void setDestination_addresses(List<String> destination_addresses) {
    this.destination_addresses = destination_addresses;
}

public List<String> getOrigin_addresses() {
    return origin_addresses;
}

public void setOrigin_addresses(List<String> origin_addresses) {
    this.origin_addresses = origin_addresses;
}

public List<Rows> getRows() {
    return rows;
}

public void setRows(List<Rows> rows) {
    this.rows = rows;
}
}

class Rows {
private List<Element> elements;

public List<Element> getElements() {
    return elements;
}

public void setElements(List<Element> elements) {
    this.elements = elements;
}
}

class Element {
private TextValue distance;
private TextValue duration;

public TextValue getDistance() {
    return distance;
}

public void setDistance(TextValue distance) {
    this.distance = distance;
}

public TextValue getDuration() {
    return duration;
}

public void setDuration(TextValue duration) {
    this.duration = duration;
}
}

class TextValue {
private String text;
private  String value;

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}
}

I essentially need to just parse the json below so that i can say (if status != ZERO_RESULTS ) save the response else throw an exception! Probably easy but im struggling! Thanks so much!

DeltaRage
  • 119
  • 1
  • 3
  • 16

1 Answers1

1

Change GoogleResponsePojo to:

 public class GoogleResponsePojo {

    private List<String> destination_addresses;
    private List<String> origin_addresses;
    private List<Rows> rows;

    public void setDestination_addresses(List<String> destination_addresses) {
        this.destination_addresses = destination_addresses;
    }

    public void setOrigin_addresses(List<String> origin_addresses) {
        this.origin_addresses = origin_addresses;
    }

    public void setRows(List<Rows> rows) {
        this.rows = rows;
    }

    //getters
}


private static class Rows{
    private List<Element> elements;

    public void setElements(List<Element> elements) {
        this.elements = elements;
    }

    //getters
}

private static class Element{
    private TextValue distance;
    private TextValue duration;

    public void setDistance(TextValue distance) {
        this.distance = distance;
    }

    public void setDuration(TextValue duration) {
        this.duration = duration;
    }

    //getters
}

private static class TextValue{
    private String text;
    private  String value;

    public void setText(String text) {
        this.text = text;
    }

    public void setValue(String value) {
        this.value = value;
    }

    //getters
}

You can take duration as following:

GoogleResponsePojo name = gson.fromJson(response.toString(),GoogleResponsePojo.class);

name.getRows().get(0).getDuration().getText();

And I recommend use http-request built on apache http api. Its simple to use:

 public static final String BASE_URL = "https://maps.googleapis.com/maps/api/distancematrix/json";

private static final HttpRequest<GoogleResponsePojo> HTTP_REQUEST =
        HttpRequestBuilder.createGet(BASE_URL, GoogleResponsePojo.class)
                .addDefaultRequestParameter("origins", "Seattle")
                .addDefaultRequestParameter("destinations", "San+Francisco")
                .addDefaultRequestParameter("key", "***")
                .build();

@GET
@Produces(MediaType.APPLICATION_JSON)
public static void updateTestExecutionDetails() throws IOException {

    ResponseHandler<GoogleResponsePojo> responseHandler = HTTP_REQUEST.execute();

    GoogleResponsePojo name = responseHandler.orElseThrow(); // throws ResponseException when status code != 200

    System.out.println(name.getDestination_addresses().get(0));
    System.out.println(name.getRows().get(0).getElements().get(0).getDuration().getText());
    System.out.println(name.getOrigin_addresses().get(0));
}
Beno
  • 945
  • 11
  • 22
  • Hi, thanks for this. However, I can still not get it to work. I recieve a ResponseException giving a 502. – DeltaRage Oct 06 '17 at 19:11
  • Tell me if it works or not. `destination_addresses` and `origin_addresses` must be `List` or `String[]`. I did not notice at once). Already corrected. – Beno Oct 06 '17 at 19:37
  • Note: I Improved the code. I changed the BASE_URL and added query string as defaut request parameters. – Beno Oct 06 '17 at 19:50
  • Thanks so much! Finally got it to work!!! Been such a ball ache! Cheers for the help! :) :) – DeltaRage Oct 06 '17 at 19:52
  • Happy to help. For info I'm developer of http-request. – Beno Oct 06 '17 at 19:54
  • Ok thanks! Is this a widely used library? Are there any advantages of using this over the way i was trying to do ? – DeltaRage Oct 06 '17 at 20:04
  • Yes. You did not parse the response manually, you did not use `if/else/`. The api provides many methods to manipulate the response. You can filter the response see documentation. – Beno Oct 06 '17 at 20:11
  • Thanks! Is it possible to send parameters parsed from a csv in the request rather than hard coding them? – DeltaRage Oct 07 '17 at 11:52
  • Yes!!! You can add parameters into method `execute` see the documentation or javadoc of methods – Beno Oct 07 '17 at 11:55
  • Thanks for the response! I have managed to do this and select data from a csv file and send it in the request. However, since this morning I have been trying to figure out a null pointer. I run the program and it will work a few times then i run it again and it gives a nullpointer exception. Im tearing my hair out with this problem and for the life of me cannot understand why. Ive edited my post to show where I am getting the null pointer. – DeltaRage Oct 08 '17 at 18:51
  • I don't see your changes! – Beno Oct 08 '17 at 19:02
  • Sorry been distracted ! They are there now – DeltaRage Oct 08 '17 at 19:03
  • It's not really a change from what you showed me which is confusing – DeltaRage Oct 08 '17 at 19:04
  • Where are NPE? You must be check the response.. example: `response.getDestination_addresses() != null` – Beno Oct 08 '17 at 19:11
  • The problem is that it tries to pass a destination address that is out of the UK giving zero results, therefore giving me a null pointer. My real weak point at coding is parsing json. The json that gets returned is listed above, can you please show me how I would get the status by modifying my GoogleResponsePojo? Im really struggling! All the updated code is above. – DeltaRage Oct 10 '17 at 18:12
  • cannot seem to do it :\ – DeltaRage Oct 10 '17 at 19:49
  • Sorry to mither but please can you help me! – DeltaRage Oct 10 '17 at 21:47