Elastic fit tutorial (3)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.