Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.kirgor.enklib.rest;
 
 
RESTful client for API with application/json content type.

Uses GSON.

public class JSONClient extends RESTClient {
    private static final Gson GSON = new Gson();

    
Creates JSONClient instance.

Parameters:
baseUrl Base URL, which all request paths will be appended to.
    public JSONClient(String baseUrl) {
        super(baseUrl"application/json");
    }

    
    @Override
    protected <T> T parseEntity(Class<T> entityClassHttpResponse httpResponsethrows IOException {
        return .fromJson(new InputStreamReader(httpResponse.getEntity().getContent()), entityClass);
    }

    
    @Override
    protected <T> List<T> parseListEntity(Class<T> entityClassHttpResponse httpResponsethrows IOException {
        JsonArray jsonArray = .fromJson(new InputStreamReader(httpResponse.getEntity().getContent()), JsonArray.class);
        ArrayList<T> result = new ArrayList<T>();
        for (JsonElement jsonElement : jsonArray) {
            result.add(.fromJson(jsonElemententityClass));
        }
        return result;
    }

    
    @Override
    protected String payloadToString(Object payload) {
        return .toJson(payload);
    }
New to GrepCode? Check out our FAQ X