Example usage for org.apache.http.entity ContentType APPLICATION_JSON

List of usage examples for org.apache.http.entity ContentType APPLICATION_JSON

Introduction

In this page you can find the example usage for org.apache.http.entity ContentType APPLICATION_JSON.

Prototype

ContentType APPLICATION_JSON

To view the source code for org.apache.http.entity ContentType APPLICATION_JSON.

Click Source Link

Usage

From source file:de.zazaz.iot.bosch.indego.App.java

public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException {
    CloseableHttpClient httpClient = HttpClients.createDefault();

    HttpPost httpPost = new HttpPost(BASE_URL_PUSHWOOSH + "registerDevice");
    String jsonPost = ""//
            + "{" //
            + "  \"request\":{" //
            + "     \"application\":\"8FF60-0666B\"," //
            + "     \"push_token\":\"124692134091\"," //
            + "     \"hwid\":\"00-0C-29-E8-B1-8D\"," //
            + "     \"timezone\":3600," //
            + "     \"device_type\":3" //
            + "  }" //
            + "}";
    httpPost.setEntity(new StringEntity(jsonPost, ContentType.APPLICATION_JSON));
    CloseableHttpResponse response = httpClient.execute(httpPost);

    System.out.println(response.getStatusLine());
    Header[] headers = response.getAllHeaders();
    for (int i = 0; i < headers.length; i++) {
        System.out.println(headers[i].getName() + ": " + headers[i].getValue());
    }/*  w  w w .  j  a  va2s  .com*/
    HttpEntity entity = response.getEntity();
    String contents = EntityUtils.toString(entity);
    System.out.println(contents);

    Thread.sleep(5000);

    HttpPost httpGet = new HttpPost(BASE_URL_PUSHWOOSH + "checkMessage");
    String jsonGet = ""//
            + "{" //
            + "  \"request\":{" //
            + "     \"application\":\"8FF60-0666B\"," //
            + "     \"hwid\":\"00-0C-29-E8-B1-8D\"" //
            + "  }" //
            + "}";
    httpGet.setEntity(new StringEntity(jsonGet, ContentType.APPLICATION_JSON));
    httpClient.execute(httpGet);

    response.close();
}

From source file:TTrestclient.TeachTimeRESTclient.java

/**
 * @param args the command line arguments
 *///from  ww w. j a v  a  2 s  .com
public static void main(String[] args) {
    TeachTimeRESTclient instance = new TeachTimeRESTclient();

    //1 -- Lista entries (JSON)
    /*System.out.println("1 -- Utente (JSON)");
    //creiamo la richiesta (GET)
    HttpGet get_request = new HttpGet(baseURI + "/users/1");
    get_request.setHeader("Accept", "application/json");
    instance.execute_and_dump(get_request);
            
    System.out.println();
            
            
            
    //6 -- Creazione  
    System.out.println("6 -- Creazione entry utente");
    HttpPost post_request = new HttpPost(baseURI + "/users");
    //per una richiesta POST, prepariamo anche il payload specificandone il tipo
    HttpEntity payload = new StringEntity(dummy_json_entry, ContentType.APPLICATION_JSON);
    //e lo inseriamo nella richiesta
    post_request.setEntity(payload);
    instance.execute_and_dump(post_request);
            
    System.out.println();*/

    HttpPost post = new HttpPost(baseURI + "/subjects/3");
    HttpEntity payload = new StringEntity("{\"nome\":\"diocane\",\"materia_key\":3}",
            ContentType.APPLICATION_JSON);
    post.setEntity(payload);
    instance.execute_and_dump(post);

}

From source file:org.aksw.simba.cetus.fox.FoxBasedTypeSearcher.java

public static void main(String[] args)
        throws UnsupportedCharsetException, ClientProtocolException, JSONException, IOException {
    Response response = Request.Post(FOX_SERVICE).addHeader("Content-type", "application/json")
            .addHeader("Accept-Charset", "UTF-8")
            .body(new StringEntity(new JSONObject().put("input",
                    "Brian Banner is a fictional villain from the Marvel Comics Universe created by Bill Mantlo and Mike Mignola and first appearing in print in late 1985.")
                    .put("type", "text").put("task", "ner").put("output", "JSON-LD").toString(),
                    ContentType.APPLICATION_JSON))
            .execute();//  www  .j a  va  2 s.com

    HttpResponse httpResponse = response.returnResponse();
    HttpEntity entry = httpResponse.getEntity();

    String content = IOUtils.toString(entry.getContent(), "UTF-8");
    System.out.println(content);
}

From source file:io.kamax.mxisd.util.RestClientUtils.java

public static HttpPost post(String url, String body) {
    StringEntity entity = new StringEntity(body, StandardCharsets.UTF_8);
    entity.setContentType(ContentType.APPLICATION_JSON.toString());
    HttpPost req = new HttpPost(url);
    req.setEntity(entity);//from  w w w  .  j a  v a2 s . co m
    return req;
}

From source file:com.twitter.heron.integration_test.core.HttpUtils.java

static int httpJsonPost(String newHttpPostUrl, String jsonData) throws IOException, ParseException {
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(newHttpPostUrl);

    StringEntity requestEntity = new StringEntity(jsonData, ContentType.APPLICATION_JSON);

    post.setEntity(requestEntity);/*w  w w. j av  a 2s  .  c  o m*/
    HttpResponse response = client.execute(post);

    return response.getStatusLine().getStatusCode();
}

From source file:org.kitodo.data.index.elasticsearch.RestClientImplementationTest.java

private static HashMap<Integer, HttpEntity> createEntities() {
    HashMap<Integer, HttpEntity> documents = new HashMap<>();

    String jsonString = "{\"title\":\"Batch1\",\"type\":\"LOGISTIC\",\"processes\":[{\"id\":\"1\"},{\"id\":\"2\"}]}";
    HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
    documents.put(1, entity);//w ww .  j  ava  2  s .  c  o m

    jsonString = "{\"title\":\"Batch2\",\"type\":\"null\",\"processes\":[]}";
    entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
    documents.put(2, entity);

    return documents;
}

From source file:org.apache.heron.integration_topology_test.core.HttpUtils.java

public static int httpJsonPost(String newHttpPostUrl, String jsonData) throws IOException, ParseException {
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(newHttpPostUrl);

    StringEntity requestEntity = new StringEntity(jsonData, ContentType.APPLICATION_JSON);

    post.setEntity(requestEntity);/*from   w  w w. j a  va 2 s  . c o m*/
    HttpResponse response = client.execute(post);

    return response.getStatusLine().getStatusCode();
}

From source file:com.xl.service.OperationService.java

public static void addSampleSink(RestClient client, String fileName) throws IOException {

    HttpEntity entity = new StringEntity(ReadExcelSampleSilk.read(fileName), ContentType.APPLICATION_JSON);
    Response indexResponse = getRestClient().performRequest("PUT", "/test/sample/1",
            Collections.<String, String>emptyMap(), entity);

    System.out/*from w  w w  .j a v a  2 s  .  co  m*/
            .println("respone :::: from server ::::::::::::::" + indexResponse.getStatusLine().getStatusCode());

}

From source file:com.ysheng.auth.common.restful.util.JsonSerializer.java

/**
 * Serializes an object to JSON string./*  w ww  .  j av a2 s. c  om*/
 *
 * @param object The object to be serialized.
 * @return A JSON string that contains the serialized object.
 * @throws JsonProcessingException The error that contains detail information.
 */
public static HttpEntity serialize(Object object) throws JsonProcessingException {
    String serialized = objectMapper.writeValueAsString(object);

    return new StringEntity(serialized, ContentType.APPLICATION_JSON);
}

From source file:com.srotya.tau.ui.BapiLoginDAO.java

public static Entry<String, String> authenticate(String authURL, String username, String password)
        throws Exception {
    CloseableHttpClient client = Utils.buildClient(authURL, 3000, 5000);
    HttpPost authRequest = new HttpPost(authURL);
    Gson gson = new Gson();
    JsonObject obj = new JsonObject();
    obj.addProperty(USERNAME, username);
    obj.addProperty(PASSWORD, password);
    StringEntity entity = new StringEntity(gson.toJson(obj), ContentType.APPLICATION_JSON);
    authRequest.setEntity(entity);//from   w ww.  ja  v a  2  s.  c o m
    CloseableHttpResponse response = client.execute(authRequest);
    if (response.getStatusLine().getStatusCode() == 200) {
        String tokenPair = EntityUtils.toString(response.getEntity());
        JsonArray ary = gson.fromJson(tokenPair, JsonArray.class);
        obj = ary.get(0).getAsJsonObject();
        String token = obj.get(X_SUBJECT_TOKEN).getAsString();
        String hmac = obj.get(HMAC).getAsString();
        return new AbstractMap.SimpleEntry<String, String>(token, hmac);
    } else {
        System.err.println("Login failed:" + response.getStatusLine().getStatusCode() + "\t"
                + response.getStatusLine().getReasonPhrase());
        return null;
    }
}