当前位置: 首页>>代码示例>>Java>>正文


Java HttpEntity.getContent方法代码示例

本文整理汇总了Java中ch.boye.httpclientandroidlib.HttpEntity.getContent方法的典型用法代码示例。如果您正苦于以下问题:Java HttpEntity.getContent方法的具体用法?Java HttpEntity.getContent怎么用?Java HttpEntity.getContent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ch.boye.httpclientandroidlib.HttpEntity的用法示例。


在下文中一共展示了HttpEntity.getContent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: toByteArray

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
/**
 * Read the contents of an entity and return it as a byte array.
 *
 * @param entity the entity to read from=
 * @return byte array containing the entity content. May be null if
 *   {@link HttpEntity#getContent()} is null.
 * @throws IOException if an error occurs reading the input stream
 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
 */
public static byte[] toByteArray(final HttpEntity entity) throws IOException {
    Args.notNull(entity, "Entity");
    final InputStream instream = entity.getContent();
    if (instream == null) {
        return null;
    }
    try {
        Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
                "HTTP entity too large to be buffered in memory");
        int i = (int)entity.getContentLength();
        if (i < 0) {
            i = 4096;
        }
        final ByteArrayBuffer buffer = new ByteArrayBuffer(i);
        final byte[] tmp = new byte[4096];
        int l;
        while((l = instream.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
        return buffer.toByteArray();
    } finally {
        instream.close();
    }
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:34,代码来源:EntityUtils.java

示例2: doConsume

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
private void doConsume() throws IOException {
    ensureNotConsumed();
    consumed = true;

    limit = new InputLimit(maxResponseSizeBytes);

    final HttpEntity entity = response.getEntity();
    if (entity == null) {
        return;
    }
    final String uri = request.getRequestLine().getUri();
    instream = entity.getContent();
    try {
        resource = resourceFactory.generate(uri, instream, limit);
    } finally {
        if (!limit.isReached()) {
            instream.close();
        }
    }
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:21,代码来源:SizeLimitedResponseReader.java

示例3: jsonObjectBody

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
/**
 * Return the body as a <b>non-null</b> <code>ExtendedJSONObject</code>.
 *
 * @return A non-null <code>ExtendedJSONObject</code>.
 *
 * @throws IllegalStateException
 * @throws IOException
 * @throws NonObjectJSONException
 */
public ExtendedJSONObject jsonObjectBody() throws IllegalStateException, IOException, NonObjectJSONException {
  if (body != null) {
    // Do it from the cached String.
    return new ExtendedJSONObject(body);
  }

  HttpEntity entity = this.response.getEntity();
  if (entity == null) {
    throw new IOException("no entity");
  }

  InputStream content = entity.getContent();
  try {
    Reader in = new BufferedReader(new InputStreamReader(content, "UTF-8"));
    return new ExtendedJSONObject(in);
  } finally {
    content.close();
  }
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:29,代码来源:MozResponse.java

示例4: jsonArrayBody

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
public JSONArray jsonArrayBody() throws NonArrayJSONException, IOException {
  final JSONParser parser = new JSONParser();
  try {
    if (body != null) {
      // Do it from the cached String.
      return (JSONArray) parser.parse(body);
    }

    final HttpEntity entity = this.response.getEntity();
    if (entity == null) {
      throw new IOException("no entity");
    }

    final InputStream content = entity.getContent();
    final Reader in = new BufferedReader(new InputStreamReader(content, "UTF-8"));
    try {
      return (JSONArray) parser.parse(in);
    } finally {
      in.close();
    }
  } catch (ClassCastException | ParseException e) {
    NonArrayJSONException exception = new NonArrayJSONException("value must be a json array");
    exception.initCause(e);
    throw exception;
  }
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:27,代码来源:MozResponse.java

示例5: getPayloadHash

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
/**
 * Generate the SHA-256 hash of a normalized Hawk payload generated from an
 * HTTP entity.
 * <p>
 * <b>Warning:</b> the entity <b>must</b> be repeatable.  If it is not, this
 * code throws an <code>IllegalArgumentException</code>.
 * <p>
 * This is under-specified; the code here was reverse engineered from the code
 * at
 * <a href="https://github.com/hueniverse/hawk/blob/871cc597973110900467bd3dfb84a3c892f678fb/lib/crypto.js#L81">https://github.com/hueniverse/hawk/blob/871cc597973110900467bd3dfb84a3c892f678fb/lib/crypto.js#L81</a>.
 * @param entity to normalize and hash.
 * @return hash.
 * @throws IllegalArgumentException if entity is not repeatable.
 */
protected static byte[] getPayloadHash(HttpEntity entity) throws UnsupportedEncodingException, IOException, NoSuchAlgorithmException {
  if (!entity.isRepeatable()) {
    throw new IllegalArgumentException("entity must be repeatable");
  }
  final MessageDigest digest = MessageDigest.getInstance("SHA-256");
  digest.update(("hawk." + HAWK_HEADER_VERSION + ".payload\n").getBytes("UTF-8"));
  digest.update(getBaseContentType(entity.getContentType()).getBytes("UTF-8"));
  digest.update("\n".getBytes("UTF-8"));
  InputStream stream = entity.getContent();
  try {
    int numRead;
    byte[] buffer = new byte[4096];
    while (-1 != (numRead = stream.read(buffer))) {
      if (numRead > 0) {
        digest.update(buffer, 0, numRead);
      }
    }
    digest.update("\n".getBytes("UTF-8")); // Trailing newline is specified by Hawk.
    return digest.digest();
  } finally {
    stream.close();
  }
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:38,代码来源:HawkAuthHeaderProvider.java

示例6: jsonObjectBody

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
/**
 * Return the body as a <b>non-null</b> <code>ExtendedJSONObject</code>.
 *
 * @return A non-null <code>ExtendedJSONObject</code>.
 *
 * @throws IllegalStateException
 * @throws IOException
 * @throws ParseException
 * @throws NonObjectJSONException
 */
public ExtendedJSONObject jsonObjectBody() throws IllegalStateException, IOException,
                               ParseException, NonObjectJSONException {
  if (body != null) {
    // Do it from the cached String.
    return ExtendedJSONObject.parseJSONObject(body);
  }

  HttpEntity entity = this.response.getEntity();
  if (entity == null) {
    throw new IOException("no entity");
  }

  InputStream content = entity.getContent();
  try {
    Reader in = new BufferedReader(new InputStreamReader(content, "UTF-8"));
    return ExtendedJSONObject.parseJSONObject(in);
  } finally {
    content.close();
  }
}
 
开发者ID:jrconlin,项目名称:mc_backup,代码行数:31,代码来源:MozResponse.java

示例7: processResponse

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
private String processResponse(HttpResponse response) throws IllegalStateException, IOException {
	// Check if server response is valid
	StatusLine status = response.getStatusLine();

	// Pull content stream from response
	HttpEntity entity = response.getEntity();
	InputStream inputStream = entity.getContent();

	ByteArrayOutputStream content = new ByteArrayOutputStream();

	// Read response into a buffered stream
	int readBytes = 0;
	byte[] sBuffer = new byte[512];
	while ((readBytes = inputStream.read(sBuffer)) != -1) {
		content.write(sBuffer, 0, readBytes);
	}

	// Return result from buffered stream
	String dataAsString = new String(content.toByteArray());

	if (status.getStatusCode() != 200) {
		throw new IOException("Invalid response from server: " + status.toString() + "\n\n" + dataAsString);
	}

	return dataAsString;
}
 
开发者ID:laurion,项目名称:wabbit-client,代码行数:27,代码来源:CommunicatorBase.java

示例8: consume

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
/**
 * Ensures that the entity content is fully consumed and the content stream, if exists,
 * is closed.
 *
 * @param entity the entity to consume.
 * @throws IOException if an error occurs reading the input stream
 *
 * @since 4.1
 */
public static void consume(final HttpEntity entity) throws IOException {
    if (entity == null) {
        return;
    }
    if (entity.isStreaming()) {
        final InputStream instream = entity.getContent();
        if (instream != null) {
            instream.close();
        }
    }
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:21,代码来源:EntityUtils.java

示例9: consume

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
static void consume(final HttpEntity entity) throws IOException {
    if (entity == null) {
        return;
    }
    if (entity.isStreaming()) {
        final InputStream instream = entity.getContent();
        if (instream != null) {
            instream.close();
        }
    }
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:12,代码来源:IOUtils.java

示例10: body

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
public String body() throws IllegalStateException, IOException {
  if (body != null) {
    return body;
  }
  final HttpEntity entity = this.response.getEntity();
  if (entity == null) {
    body = null;
    return null;
  }

  InputStreamReader is = new InputStreamReader(entity.getContent(), StringUtils.UTF_8);
  // Oh, Java, you are so evil.
  body = new Scanner(is).useDelimiter("\\A").next();
  return body;
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:16,代码来源:MozResponse.java

示例11: handleResponse

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
public Multistatus handleResponse(HttpResponse response) throws SardineException, IOException
	{
		super.validateResponse(response);

		// Process the response from the server.
		HttpEntity entity = response.getEntity();
		StatusLine statusLine = response.getStatusLine();
		if (entity == null)
		{
			throw new SardineException("No entity found in response", statusLine.getStatusCode(),
					statusLine.getReasonPhrase());
		}
		
//		boolean debug=false;
//		//starn: for debug purpose, display the stream on the console
//		if (debug){
//			String result = convertStreamToString(entity.getContent());
//			System.out.println(result);
//			InputStream in = new ByteArrayInputStream(result.getBytes()); 
//			return this.getMultistatus(in);
//		}
//		else {
//			return this.getMultistatus(entity.getContent());
//		}
		Multistatus m = new Multistatus();
		new XmlMapper(entity.getContent(),m);
		return m;
	}
 
开发者ID:starn,项目名称:encdroidMC,代码行数:29,代码来源:MultiStatusResponseHandler.java

示例12: ConsumingInputStream

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
/**
 * @param response The HTTP response to read from
 * @throws IOException		  If there is a problem reading from the response
 * @throws NullPointerException If the response has no message entity
 */
public ConsumingInputStream(final HttpResponse response) throws IOException
{
	this.response = response;
	HttpEntity entity = response.getEntity();
	this.delegate = entity.getContent();
}
 
开发者ID:starn,项目名称:encdroidMC,代码行数:12,代码来源:ConsumingInputStream.java

示例13: entityToString

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
protected static String entityToString(HttpEntity entity) throws IOException {
  BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
  StringBuilder builder = new StringBuilder();
  for (String line = ""; line != null; line = reader.readLine()) {
    builder.append(line);
  }
  return builder.toString();
}
 
开发者ID:jrconlin,项目名称:mc_backup,代码行数:9,代码来源:GCM.java

示例14: body

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
public String body() throws IllegalStateException, IOException {
  if (body != null) {
    return body;
  }
  final HttpEntity entity = this.response.getEntity();
  if (entity == null) {
    body = null;
    return null;
  }

  InputStreamReader is = new InputStreamReader(entity.getContent());
  // Oh, Java, you are so evil.
  body = new Scanner(is).useDelimiter("\\A").next();
  return body;
}
 
开发者ID:jrconlin,项目名称:mc_backup,代码行数:16,代码来源:MozResponse.java

示例15: toString

import ch.boye.httpclientandroidlib.HttpEntity; //导入方法依赖的package包/类
/**
 * Get the entity content as a String, using the provided default character set
 * if none is found in the entity.
 * If defaultCharset is null, the default "ISO-8859-1" is used.
 *
 * @param entity must not be null
 * @param defaultCharset character set to be applied if none found in the entity
 * @return the entity content as a String. May be null if
 *   {@link HttpEntity#getContent()} is null.
 * @throws ParseException if header elements cannot be parsed
 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
 * @throws IOException if an error occurs reading the input stream
 * @throws UnsupportedCharsetException Thrown when the named charset is not available in
 * this instance of the Java virtual machine
 */
public static String toString(
        final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
    Args.notNull(entity, "Entity");
    final InputStream instream = entity.getContent();
    if (instream == null) {
        return null;
    }
    try {
        Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
                "HTTP entity too large to be buffered in memory");
        int i = (int)entity.getContentLength();
        if (i < 0) {
            i = 4096;
        }
        Charset charset = null;
        try {
            final ContentType contentType = ContentType.get(entity);
            if (contentType != null) {
                charset = contentType.getCharset();
            }
        } catch (final UnsupportedCharsetException ex) {
            throw new UnsupportedEncodingException(ex.getMessage());
        }
        if (charset == null) {
            charset = defaultCharset;
        }
        if (charset == null) {
            charset = HTTP.DEF_CONTENT_CHARSET;
        }
        final Reader reader = new InputStreamReader(instream, charset);
        final CharArrayBuffer buffer = new CharArrayBuffer(i);
        final char[] tmp = new char[1024];
        int l;
        while((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
        return buffer.toString();
    } finally {
        instream.close();
    }
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:57,代码来源:EntityUtils.java


注:本文中的ch.boye.httpclientandroidlib.HttpEntity.getContent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。