Example usage for org.apache.http.util EntityUtils toString

List of usage examples for org.apache.http.util EntityUtils toString

Introduction

In this page you can find the example usage for org.apache.http.util EntityUtils toString.

Prototype

public static String toString(HttpEntity httpEntity) throws IOException, ParseException 

Source Link

Usage

From source file:org.fcrepo.integration.syndication.RSSIT.java

@Test
public void testRSS() throws Exception {

    final Session session = repo.login();
    objectService.createObject(session, "/RSSTESTPID");
    session.save();//from   ww w  . j  av a  2  s.  com
    session.logout();

    HttpGet getRSSMethod = new HttpGet(serverAddress + "/fcr:rss");
    HttpResponse response = client.execute(getRSSMethod);
    assertEquals(200, response.getStatusLine().getStatusCode());
    String content = EntityUtils.toString(response.getEntity());
    logger.debug("Retrieved RSS feed:\n" + content);
    assertTrue("Didn't find the test PID in RSS!", compile("RSSTESTPID", DOTALL).matcher(content).find());
}

From source file:org.dasein.cloud.utils.requester.DaseinResponseHandler.java

@Override
public T handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException {
    if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK
            && httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT
            && httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED
            && httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_ACCEPTED) {
        throw new HttpResponseException(httpResponse.getStatusLine().getStatusCode(),
                EntityUtils.toString(httpResponse.getEntity()));
    } else {/*  w w w . j  ava2  s .  c  o  m*/
        if (httpResponse.getEntity() == null)
            return null;

        return processor.read(httpResponse.getEntity().getContent(), classType);
    }
}

From source file:org.kitodo.data.elasticsearch.index.type.AuthorityTypeTest.java

@Test
public void shouldCreateFirstDocument() throws Exception {
    AuthorityType authorityType = new AuthorityType();

    Authority authority = prepareData().get(0);
    HttpEntity document = authorityType.createDocument(authority);

    JsonObject actual = Json.createReader(new StringReader(EntityUtils.toString(document))).readObject();

    assertEquals("Key title doesn't match to given value!", "First",
            AuthorityTypeField.TITLE.getStringValue(actual));

    JsonArray userGroups = actual.getJsonArray(AuthorityTypeField.USER_GROUPS.getKey());
    assertEquals("Size userGroups doesn't match to given value!", 1, userGroups.size());

    JsonObject userGroup = userGroups.getJsonObject(0);
    assertEquals("Key userGroups.id doesn't match to given value!", 1,
            UserGroupTypeField.ID.getIntValue(userGroup));
    assertEquals("Key userGroups.title doesn't match to given value!", "First",
            UserGroupTypeField.TITLE.getStringValue(userGroup));
}

From source file:forplay.android.AndroidNet.java

private void doHttp(boolean isPost, String url, String data, Callback callback) {
    // TODO: use AsyncTask
    HttpClient httpclient = new DefaultHttpClient();
    HttpRequestBase req = null;/*  w w w .j a v a2s . c  om*/
    if (isPost) {
        HttpPost httppost = new HttpPost(url);
        if (data != null) {
            try {
                httppost.setEntity(new StringEntity(data));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                callback.failure(e);
            }
        }
        req = httppost;
    } else {
        req = new HttpGet(url);
    }
    try {
        HttpResponse response = httpclient.execute(req);
        callback.success(EntityUtils.toString(response.getEntity()));
    } catch (Exception e) {
        callback.failure(e);
    }
}

From source file:com.microsoft.live.JsonResponseHandler.java

@Override
public JSONObject handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    final HttpEntity entity = response.getEntity();
    final String stringResponse;
    if (entity != null) {
        stringResponse = EntityUtils.toString(entity);
    } else {/*from   ww w . j  a  v a2  s .  c  om*/
        return null;
    }

    if (TextUtils.isEmpty(stringResponse)) {
        return new JSONObject();
    }

    try {
        return new JSONObject(stringResponse);
    } catch (JSONException e) {
        throw new IOException(e.getLocalizedMessage());
    }
}

From source file:android.hawkencompanionapp.asynctasks.LoadMechInfoTask.java

protected Void doInBackground(Void... v) {
    final HttpPost httpPost = new HttpPost(mUrl);
    final HttpClient httpClient = new DefaultHttpClient();
    try {// www . ja  va 2  s.c om
        final HttpResponse httpResponse = httpClient.execute(httpPost);
        final String responseStr = EntityUtils.toString(httpResponse.getEntity());
        mMechType.setHtmlData(responseStr);
    } catch (IOException e) {
        Logger.error(this, e.getMessage());
    }
    return null;
}

From source file:models.ApiConnector.java

public JSONArray GetAllTeas() {
    String url = "http://chuangmi.my-place.us/getAllTeas.php";

    /*//  w ww.  j  a  va2 s .co  m
     * Get HttpResponse object from the url
     * Get HttpEntity from Http Response object
     */

    HttpEntity httpEntity = null;

    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);

        httpEntity = httpResponse.getEntity();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //convert HttpEntity into JSON array
    JSONArray jsonArray = null;

    if (httpEntity != null) {
        try {
            String entityResponse = EntityUtils.toString(httpEntity);
            Log.e("Entity Response : ", entityResponse);
            jsonArray = new JSONArray(entityResponse);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return jsonArray;
}

From source file:juzu.impl.plugin.asset.ETagTestCase.java

@Test
public void testSatisfied() throws Exception {

    driver.get(applicationURL().toString());
    WebElement element = driver.findElement(By.tagName("script"));
    String assetURI = element.getAttribute("src");
    assertNotNull(assetURI);/*from  w  ww. j a  v a2  s  .  c o  m*/
    HttpClient client = HttpClientBuilder.create().build();

    // With no etag
    HttpGet get = new HttpGet(assetURI);
    HttpResponse response = client.execute(get);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals("a = 0;", EntityUtils.toString(response.getEntity()));
    Header[] etag = response.getHeaders("ETag");
    assertNotNull(etag);
    assertEquals(1, etag.length);

    // With correct etag
    get = new HttpGet(assetURI);
    get.setHeader("If-None-Match", etag[0].getValue());
    response = client.execute(get);
    assertEquals(304, response.getStatusLine().getStatusCode());
    assertEquals(null, response.getEntity());

    // With wrong etag
    get = new HttpGet(assetURI);
    get.setHeader("If-None-Match", "foo");
    response = client.execute(get);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals("a = 0;", EntityUtils.toString(response.getEntity()));
    etag = response.getHeaders("ETag");
    assertNotNull(etag);
}

From source file:com.ninehcom.userinfo.agent.SearchAgent.java

public boolean updateSearchWord(UserInfo user) throws Exception {
    if (searchURL != null) {
        try {/*  w w w  .  j  a  v a 2  s .c o  m*/
            String host = searchURL;
            Map<String, String> header = new HashMap<>();
            header.put("accept", "*/*");
            header.put("connection", "Keep-Alive");
            header.put("user-agent",
                    "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13");
            header.put("Content-Type", "application/json");
            Map<String, String> bodys = new HashMap<>();
            bodys.put("id", user.getId());
            bodys.put("nickName", user.getNickName());
            bodys.put("avatar", user.getPortrait());
            bodys.put("signature", user.getSignature());
            bodys.put("birthday", user.getBirthday() == null ? null : format.format(user.getBirthday()));
            bodys.put("gender", user.getSex() + "");
            bodys.put("updatedAt", format.format(new Date()));
            HttpResponse response = HttpUtils.doPost(host, "", "", header, null, bodys);
            String jsonResultStr = EntityUtils.toString(response.getEntity());
        } catch (Exception ex) {
            return false;
        }
    }
    return true;
}