當前位置: 首頁>>代碼示例>>Java>>正文


Java TypedInput.mimeType方法代碼示例

本文整理匯總了Java中retrofit.mime.TypedInput.mimeType方法的典型用法代碼示例。如果您正苦於以下問題:Java TypedInput.mimeType方法的具體用法?Java TypedInput.mimeType怎麽用?Java TypedInput.mimeType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在retrofit.mime.TypedInput的用法示例。


在下文中一共展示了TypedInput.mimeType方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getResponseString

import retrofit.mime.TypedInput; //導入方法依賴的package包/類
/**
 * Here we take the RetroFit Response and convert it to a String
 *
 * TODO: use gson instead
 *
 * @param response Response object
 * @return The Response body converted to a String
 */
public static String getResponseString(Response response) {
    try {
        TypedInput body = response.getBody();
        byte[] bodyBytes = ((TypedByteArray) body).getBytes();
        String bodyMime = body.mimeType();
        String bodyCharset = MimeUtil.parseCharset(bodyMime, "UTF-8");
        return new String(bodyBytes, bodyCharset);
    } catch (UnsupportedEncodingException ue) {
        Log.e("Exception", ue.getMessage());
        return "Error";
    } catch (NullPointerException npe) {
        Log.e("Exception", npe.getMessage());
        return "Error";
    }

}
 
開發者ID:stinsonga,項目名稱:GSApp,代碼行數:25,代碼來源:GeoUtils.java

示例2: fromBody

import retrofit.mime.TypedInput; //導入方法依賴的package包/類
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
    String charset = "UTF-8";
    if (body.mimeType() != null) {
        charset = MimeUtil.parseCharset(body.mimeType());
    }
    InputStreamReader isr = null;
    try {
        isr = new InputStreamReader(body.in(), charset);
        return gson.fromJson(isr, type);
    } catch (IOException | JsonParseException e) {
        throw new ConversionException(e);
    } finally {
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException ex) {
                // Ignored
            }
        }
    }
}
 
開發者ID:phil-lopreiato,項目名稱:tba-apiv2-java,代碼行數:23,代碼來源:RetrofitConverter.java

示例3: fromBody

import retrofit.mime.TypedInput; //導入方法依賴的package包/類
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
    String charset = "UTF-8";
    if (body.mimeType() != null) {
        charset = MimeUtil.parseCharset(body.mimeType());
    }

    InputStreamReader isr = null;
    try {
        isr = new InputStreamReader(body.in(), charset);

        // Need a Class instance here, as using the Type in serializer.read(...) doesn't work
        Class<?> typeClass = (Class<?>) type;

        return serializer.read((Class<?>) type, isr);
    } catch (Exception e) {
        throw new ConversionException(e);
    }
}
 
開發者ID:aegis123,項目名稱:retrofit-simplexmlconverter,代碼行數:20,代碼來源:SimpleXmlConverter.java

示例4: getString

import retrofit.mime.TypedInput; //導入方法依賴的package包/類
private String getString(TypedInput body) throws ConversionException {
    String charset = CHARSET_DEFAULT;
    if (body.mimeType() != null) {
        charset = MimeUtil.parseCharset(body.mimeType(), charset);
    }
    InputStreamReader isr = null;
    String result;
    try {
        isr = new InputStreamReader(body.in(), charset);
        StringBuilder sb = new StringBuilder();
        char[] buf = new char[BUFFER_SIZE];
        for (; ; ) {
            int res = isr.read(buf, 0, buf.length);
            if (res < 0) {
                break;
            }
            sb.append(buf, 0, res);
        }
        result = sb.toString();
        return result;
    } catch (IOException e) {
        throw new ConversionException(e);
    } finally {
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException ignored) {
            }
        }
    }
}
 
開發者ID:Jude95,項目名稱:Fishing,代碼行數:32,代碼來源:WrapperConverter.java

示例5: getBodyString

import retrofit.mime.TypedInput; //導入方法依賴的package包/類
public static String getBodyString(Response response) throws IOException {
    TypedInput body = response.getBody();
    if (body != null) {
        byte[] bodyBytes = ((TypedByteArray) body).getBytes();
        String bodyMime = body.mimeType();
        String bodyCharset = MimeUtil.parseCharset(bodyMime);
        return new String(bodyBytes, bodyCharset);
    }
    return null;
}
 
開發者ID:padc,項目名稱:DevConSummit,代碼行數:11,代碼來源:Util.java

示例6: logAndReplaceResponse

import retrofit.mime.TypedInput; //導入方法依賴的package包/類
/**
 * Log response headers and body. Consumes response body and returns identical replacement.
 */
private Response logAndReplaceResponse(String url, Response response, long elapsedTime) throws IOException {
    log.log(String.format("<--- HTTP %s %s (%sms)", response.getStatus(), url, elapsedTime));

    if (logLevel.ordinal() >= LogLevel.HEADERS.ordinal()) {
        for (Header header : response.getHeaders()) {
            log.log(header.getName() + ": " + header.getValue());
        }

        long bodySize = 0;
        TypedInput body = response.getBody();
        if (body != null) {
            bodySize = body.length();

            if (logLevel.ordinal() >= LogLevel.FULL.ordinal()) {
                if (!response.getHeaders().isEmpty()) {
                    log.log("");
                }

                if (!(body instanceof TypedByteArray)) {
                    // Read the entire response body to we can log it and replace the original response
                    response = Utils.readBodyToBytesIfNecessary(response);
                    body = response.getBody();
                }

                byte[] bodyBytes = ((TypedByteArray) body).getBytes();
                bodySize = bodyBytes.length;
                String bodyMime = body.mimeType();
                String bodyCharset = MimeUtil.parseCharset(bodyMime);
                String bodyString = new String(bodyBytes, bodyCharset);
                for (int i = 0, len = bodyString.length(); i < len; i += LOG_CHUNK_SIZE) {
                    int end = Math.min(len, i + LOG_CHUNK_SIZE);
                    log.log(bodyString.substring(i, end));
                }
            }
        }

        log.log(String.format("<--- END HTTP (%s-byte body)", bodySize));
    }

    return response;
}
 
開發者ID:goodev,項目名稱:android-discourse,代碼行數:45,代碼來源:RestAdapter.java

示例7: logAndReplaceResponse

import retrofit.mime.TypedInput; //導入方法依賴的package包/類
/**
 * Log response headers and body. Consumes response body and returns identical replacement.
 */
private Response logAndReplaceResponse(String url, Response response, long elapsedTime)
    throws IOException
{
  log.log(String.format("<--- HTTP %s %s (%sms)", response.getStatus(), url, elapsedTime));

  if (logLevel.ordinal() >= LogLevel.HEADERS.ordinal())
  {
    for (Header header : response.getHeaders())
    {
      log.log(header.toString());
    }

    long bodySize = 0;
    TypedInput body = response.getBody();
    if (body != null)
    {
      bodySize = body.length();

      if (logLevel.ordinal() >= LogLevel.FULL.ordinal())
      {
        if (!response.getHeaders().isEmpty())
        {
          log.log("");
        }

        if (!(body instanceof TypedByteArray))
        {
          // Read the entire response body so we can log it and replace the original response
          response = Utils.readBodyToBytesIfNecessary(response);
          body = response.getBody();
        }

        byte[] bodyBytes = ((TypedByteArray) body).getBytes();
        bodySize = bodyBytes.length;
        String bodyMime = body.mimeType();
        String bodyCharset = MimeUtil.parseCharset(bodyMime);
        log.log(new String(bodyBytes, bodyCharset));
      }
    }

    log.log(String.format("<--- END HTTP (%s-byte body)", bodySize));
  }

  return response;
}
 
開發者ID:toadzky,項目名稱:retrofit-jaxrs,代碼行數:49,代碼來源:RestAdapter.java

示例8: fromBody

import retrofit.mime.TypedInput; //導入方法依賴的package包/類
@SuppressWarnings("unchecked") //
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException
{
  if (!(type instanceof Class<?>))
  {
    throw new IllegalArgumentException("Expected a raw Class<?> but was " + type);
  }
  Class<?> c = (Class<?>) type;
  if (!Message.class.isAssignableFrom(c))
  {
    throw new IllegalArgumentException("Expected a proto message but was " + c.getName());
  }

  if (!MIME_TYPE.equalsIgnoreCase(body.mimeType()))
  {
    throw new IllegalArgumentException("Expected a proto but was: " + body.mimeType());
  }

  InputStream in = null;
  try
  {
    in = body.in();
    return wire.parseFrom(in, (Class<Message>) c);
  }
  catch (IOException e)
  {
    throw new ConversionException(e);
  }
  finally
  {
    if (in != null)
    {
      try
      {
        in.close();
      }
      catch (IOException ignored)
      {
      }
    }
  }
}
 
開發者ID:toadzky,項目名稱:retrofit-jaxrs,代碼行數:44,代碼來源:WireConverter.java


注:本文中的retrofit.mime.TypedInput.mimeType方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。