2017-10-06 97 views
0

此json發生在目標或原點位於英國之外時,因此不會給我任何結果!我需要檢查這對空校驗,所以我不接受空指針異常分析從Google距離矩陣API發送的Json響應

JSON結果:

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

「狀態」: 「OK」 }

代碼:

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); 
} 

作爲json的結構的Google Reponse POJO:

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; 
} 
} 

我基本上只需要解析下面的json,以便我可以說(如果status!= ZERO_RESULTS)保存響應,否則拋出異常!可能很容易,但即時通訊掙扎!非常感謝!

+0

你可以添加響應作爲字符串沒有解析? –

+0

你是什麼意思對不起?將響應添加到Gson Constrctor? – Josh

+0

不,我想看看'response.toString()'的結果' –

回答

0

更改GoogleResponsePojo到:

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 
} 

您可以duration如下:

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

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

,我建議使用http-request它基於Apache HTTP API。它的使用簡單:

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)); 
} 
+0

嗨,謝謝你。但是,我仍然無法使其工作。我收到一個ResponseException給出一個502. – Josh

+0

嗨,我糾正了代碼。 –

+0

告訴我,如果它的工作與否。 'destination_addresses'和'origin_addresses'必須是'List '或'String []'。我沒有立即注意到)。已經更正。 –