简体   繁体   English

google-http-java-client json更新现有对象

[英]google-http-java-client json update existing object

i'm trying to use google-http-java-client on android and parse JSON responses from my server. 我正在尝试在Android上使用google-http-java-client并解析来自我服务器的JSON响应。 do do that i'm using the following code (provided by the examples of the project) 这样做是使用以下代码(由项目示例提供)

    private static final HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
    private static final JsonFactory JSON_FACTORY = new JacksonFactory();
    HttpRequestFactory requestFactory = HTTP_TRANSPORT
                .createRequestFactory(new HttpRequestInitializer() {
                    @Override
                    public void initialize(HttpRequest request) {
                        request.setParser(new JsonObjectParser(JSON_FACTORY));
                    }
                });
    HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(url + paramsList));
    HttpResponse response = request.execute();

and everything works fine for new objects with 一切都适用于新对象

result = response.parseAs(PxUser.class);

but i need to update an existing object with the data from the json string. 但是我需要使用json字符串中的数据更新现有对象。 with jackson only i can use the following code but with the google client i cannot find any solution. 只有杰克逊,我可以使用以下代码,但对于Google客户端,我找不到任何解决方案。

InputStream in = -get-http-reponse-
ObjectMapper mapper = new ObjectMapper();
ObjectReader reader = mapper.readerForUpdating(MySingleton.getInstance());
reader.readValue(InputStream in);

so i need a way to update an existing object just like with this jackson example but by using the client. 所以我需要一种方法来更新现有对象,就像这个杰克逊示例一样,但是要使用客户端。

is there a way? 有办法吗? do i have to use jackson-databind.jar? 我必须使用jackson-databind.jar吗? how can i accomplish this? 我怎样才能做到这一点? thanks in advance 提前致谢

PS: i can switch to gson if its needed, no problem PS:如果需要,我可以切换到gson,没问题

It depends on whatever endpoint is receiving the API call, and what it expects the request to look like. 它取决于接收API调用的端点,以及该请求的外观。

The Google HTTP Java Client simply handles the processes like making the call, encoding and decoding an object, exponential backoff, etc for you. Google HTTP Java客户端可以为您处理呼叫,对对象进行编码和解码,指数补偿等过程。 It's up to you to create the request that does what you want and how the server expects it to look. 由您决定创建满足您要求的请求以及服务器期望其外观的请求。

Likely, the API you're making the request to expects an object update to be made with a PUT request. 您发出请求的API可能希望通过PUT请求进行对象更新。 The updated object is likely going to be the content of the request, encoded in some specific format. 更新后的对象可能是请求的内容,以某种特定格式编码。 Let's assume JSON, since you're parsing JSON responses. 让我们假设JSON,因为您正在解析JSON响应。 So for the purpose of example, let's say you're going to request an object, modify it, then send it back. 因此,出于示例目的,假设您要请求一个对象,对其进行修改,然后将其发送回去。

First, you get the resource and parse it into your object: 首先,获取资源并将其解析为对象:

PxUser myUser = response.parseAs(PxUser.class);

Then you modify the object somehow 然后以某种方式修改对象

myUser.setName("Frodo Baggins");

Now you want to send it back to the server as a JSON object in a PUT request: 现在,您要将其作为PUT请求中的JSON对象发送回服务器:

// httpbin.org is a wonderful URL to test API calls against as it returns whatever if received.
GenericUrl url = new GenericUrl("http://httpbin.org/put");
JsonHttpContent content = new JsonHttpContent(new JacksonFactory(), myUser);
HttpRequest request = requestFactory.buildPutRequest(url, content);
HttpResponse response = request.execute();
System.out.println(response.parseAsString());

The specifics of how you encode and update your content is totally up to you and the API's specification. 编码和更新内容的方式完全取决于您和API的规范。 This is especially easy if you're creating the server receiving the call too. 如果要创建也接收呼叫的服务器,这特别容易。

If you're working with a preexisting API, you may want to update the question with the specific problem (API "x" requires a response that looks like Blah; how do I do that in the google-http-java-client). 如果您使用的是预先存在的API,则可能要更新特定问题的问题(API“ x”要求的响应看起来像Blah;如何在google-http-java-client中执行此操作)。

If you're working with a Google API, you'll want to be using the google-api-java-client which does all of this work for you. 如果您使用的是Google API,则需要使用google-api-java-client ,它可以为您完成所有这些工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM