RequestinterceptorRequest interceptor
This can be done for repeated network access requests.
RequestInterceptor requestInterceptor = new RequestInterceptor() { @Override public void intercept(RequestFacade request) { request.addHeader("User-Agent", "Retrofit-Sample-App"); }};
RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://api.github.com") .setRequestInterceptor(requestInterceptor) .build();
ErrorIf you need to customize a network access error handling method, you need to customize an errorhandler. The following code throws a custom exception when the returned code is 401.
Class myerrorhandler implements errorhandler {@ override public throwable handleerror (writable fiterror cause) {response r = cause. getresponse (); If (R! = NULL & R. getstatus () = 401) {return New unauthorizedexception (cause);} return cause; // the return value cannot be null; otherwise, an exception occurs .}}
RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://api.github.com") .setErrorHandler(new MyErrorHandler()) .build();
Note that when the return code for network access is 40x, 50x, or ioexception, it will be converted to writable fiterror and thrown again. because the specified fiterror is runtimeexception. the user is not prompted to capture. therefore, the app crashes on the Android platform. the solution is not to use the synchronous method, but the callback Asynchronous Method. the exception can be properly handled in the callback failure () method. will not be thrown again
LoggingYou can view the request and response in detail by adding a logging level. logging levels include basic, full, headers, and none. the headers of the request and response will be printed at the full level. body, metadata, etc.
RestAdapter restAdapter = new RestAdapter.Builder() .setLogLevel(RestAdapter.LogLevel.FULL) .setEndpoint("https://api.github.com") .build();
Unlike the interceptor or errorhandler, you can use setloglevel () to add or change the logging level at any time during the restadapter lifecycle.
Jacksonconverter
If you want to use Jackson instead of gson, you can customize the following converter.
public class JacksonConverter implements Converter {// Constructor...@Overridepublic Object fromBody(TypedInput body, Type type) throws ConversionException {JavaType javaType = objectMapper.getTypeFactory().constructType(type);try {return objectMapper.readValue(body.in(), javaType);}catch (IOException e) {throw new ConversionException(e);}}@Overridepublic TypedOutput toBody(Object object) {try {String charset = "UTF-8";return new JsonTypedOutput(objectMapper.writeValueAsString(object).getBytes(charset), charset);}catch (IOException e) {throw new AssertionError(e);}}...// JsonTypedOutput implementation}
private static class JsonTypedOutput implements TypedOutput { private final byte[] jsonBytes; private final String mimeType; JsonTypedOutput(byte[] jsonBytes, String encode) { this.jsonBytes = jsonBytes; this.mimeType = "application/json; charset=" + encode; } @Override public String fileName() { return null; } @Override public String mimeType() { return mimeType; } @Override public long length() { return jsonBytes.length; } @Override public void writeTo(OutputStream out) throws IOException { out.write(jsonBytes); } }
Elastic fit tutorial (3)