首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与Jackson Parser

与Jackson Parser
EN

Code Review用户
提问于 2017-07-27 18:51:58
回答 2查看 1K关注 0票数 5

我正在开发Google,用下面的代码片段进行反向地理编码。

我所用的是:

  • 春天
  • 杰克逊·帕瑟
  • Google

它工作得很好,但效果有点慢。我能提高我的代码的性能吗?

代码语言:javascript
复制
package com.addressparser.api;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class GeocodeAddressParser {
    public String getLocationInfo( String lat, String lng) throws JsonProcessingException, IOException {

        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=false");

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
            } catch (IOException e) {
        }

        ObjectMapper mapper = new ObjectMapper();
        JsonNode array =  mapper.readValue(stringBuilder.toString(), JsonNode.class);
        JsonNode object = array.get("results").get(0);
        String reportKey = object.get("formatted_address").textValue();
        System.out.println(reportKey);//just printing it on the console for testing at the moment
        return reportKey;

    }


    public static void main(String[] args) throws JsonProcessingException, IOException{
        GeocodeAddressParser ref = new GeocodeAddressParser();

        ref.getLocationInfo("31.338451","75.554183"); 
    }
}
EN

回答 2

Code Review用户

发布于 2017-07-27 21:52:36

永远不要像这样默默地吞下例外:

} catch (ClientProtocolException e) {} catch (IOException e) {}

因此,调试隐藏的问题需要花费大量的时间。至少,将一些诊断消息打印到System.err。此外,您无法负责任地继续尝试解析一个不完整的结果,因此您应该中止处理。

基本上,如果您不知道如何处理异常,您可能应该传播它。事实上,您已经声明getLocationInfo()可以抛出一个IOException,那么为什么要费心捕捉IOException呢?

票数 1
EN

Code Review用户

发布于 2018-07-24 14:09:13

因为这是一个旧的帖子,只是一些简短的评论:

  • 方法是做太多不同的事情。至少,HTTP请求和JSON解析应该分开。
  • 没有必要自己将InputStream读入StringBuilder。杰克逊的ObjectMapper#readValue可以直接从InputStream中读取。
  • 如果您实际获得了预期的JSON结构,则不执行任何检查。至少,使用JsonNode#path(String)JsonNode#path(int)而不是get来避免获取nullNullPointerException

编辑:最后一件事:既然您正在使用Spring,那么请看一看RestTemplate。它在一个步骤中使用Jackson执行HTTP请求和JSON解析:

代码语言:javascript
复制
ObjectNode result = restTemplate.getForObject("http://maps.google.com/maps/api/geocode/json?latlng={latlang}&sensor=false", ObjectNode.class, lat+","+lng);
JsonNode object = result.get("results").get(0);
// etc.
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/171343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档