Retrofit讲解;
在serviceMethod这个方法中:是对http请求参数的拼接
在build方法中:
int parameterCount =parameterAnnotationsArray.length;
parameterHandlers =new ParameterHandler[parameterCount];
//遍历方法中的参数
for (int p =0; p < parameterCount; p++) {
Type parameterType =parameterTypes[p];
if (Utils.hasUnresolvableType(parameterType)) {
throw parameterError(p,"Parameter type must not include a type variable or wildcard: %s",
parameterType);
}
//获取所有参数的注解
Annotation[] parameterAnnotations =parameterAnnotationsArray[p];
if (parameterAnnotations ==null) {
throw parameterError(p,"No Retrofit annotation found.");
}
//获取参数的注解值,参数值
parameterHandlers[p] = parseParameter(p, parameterType, parameterAnnotations);
}
//解析参数中的所有注解(嵌套循环)
private ParameterHandler parseParameter(
int p, Type parameterType, Annotation[] annotations) {
ParameterHandler result =null;
for (Annotation annotation : annotations) {
ParameterHandler annotationAction = parseParameterAnnotation(
p, parameterType, annotations, annotation);
if (annotationAction ==null) {
continue;
}
if (result !=null) {
throw parameterError(p,"Multiple Retrofit annotations found, only one allowed.");
}
result = annotationAction;
}
if (result ==null) {
throw parameterError(p,"No Retrofit annotation found.");
}
return result;
}
//解析方法的注解,请求的方式可能时post,get,put,delete等
private void parseMethodAnnotation(Annotation annotation) {
if (annotationinstanceof DELETE) {
parseHttpMethodAndPath("DELETE", ((DELETE) annotation).value(),false);
}else if (annotationinstanceof GET) {
parseHttpMethodAndPath("GET", ((GET) annotation).value(),false);
}else if (annotationinstanceof HEAD) {
parseHttpMethodAndPath("HEAD", ((HEAD) annotation).value(),false);
if (!Void.class.equals(responseType)) {
throw methodError("HEAD method must use Void as response type.");
}
}else if (annotationinstanceof PATCH) {
parseHttpMethodAndPath("PATCH", ((PATCH) annotation).value(),true);
}else if (annotationinstanceof POST) {
parseHttpMethodAndPath("POST", ((POST) annotation).value(),true);
}else if (annotationinstanceof PUT) {
parseHttpMethodAndPath("PUT", ((PUT) annotation).value(),true);
}else if (annotationinstanceof OPTIONS) {
parseHttpMethodAndPath("OPTIONS", ((OPTIONS) annotation).value(),false);
}else if (annotationinstanceof HTTP) {
HTTP http = (HTTP) annotation;
parseHttpMethodAndPath(http.method(), http.path(), http.hasBody());
}else if (annotationinstanceof retrofit2.http.Headers) {
String[] headersToParse = ((retrofit2.http.Headers) annotation).value();
if (headersToParse.length ==0) {
throw methodError("@Headers annotation is empty.");
}
headers = parseHeaders(headersToParse);
}else if (annotationinstanceof Multipart) {
if (isFormEncoded) {
throw methodError("Only one encoding annotation is allowed.");
}
isMultipart =true;
}else if (annotationinstanceof FormUrlEncoded) {
if (isMultipart) {
throw methodError("Only one encoding annotation is allowed.");
}
isFormEncoded =true;
}
}
private void parseHttpMethodAndPath(String httpMethod, String value,boolean hasBody) {
if (this.httpMethod !=null) {
throw methodError("Only one HTTP method is allowed. Found: %s and %s.",
this.httpMethod, httpMethod);
}
//http请求的方式
this.httpMethod = httpMethod;
//是否有请求体
this.hasBody = hasBody;
if (value.isEmpty()) {
return;
}
// Get the relative URL path and existing query string, if present.
int question = value.indexOf('?');
if (question != -1 && question < value.length() -1) {
// Ensure the query string does not have any named parameters.
String queryParams = value.substring(question +1);
Matcher queryParamMatcher =PARAM_URL_REGEX.matcher(queryParams);
if (queryParamMatcher.find()) {
throw methodError("URL query string \"%s\"must not have replace block. "
+"For dynamic query parameters use @Query.", queryParams);
}
}
this.relativeUrl = value;
//方法注解的值,用来请求拼装
this.relativeUrlParamNames =parsePathParameters(value);
}