Retrofit restful client(二)基本例子

引入Retrofit

引入Retrofitt需要引入以下lib:

        
		
			maven-restlet
			Public online Restlet repository
			http://maven.restlet.org
		
	
        
			com.squareup.retrofit
			retrofit
			1.3.0
		
		
			org.apache.httpcomponents
			httpcore
			4.3
		
		
			org.apache.httpcomponents
			httpclient
			4.3.1
		
		
			org.restlet.jee
			org.restlet
			${restlet.version}
		
		
		
			org.codehaus.jackson
			jackson-mapper-asl
			1.9.13
		
		
如果不是maven工程请下载以上的jar。


Retrofit转换器(JSON<->OBJECT)

通过转换器实现JSON和对象转换,无须手动编写大量业务源代码。REST的server的输入Object可以直接转换到Client的对象。

import java.io.IOException;


public class JacksonConverter implements Converter {
	private final ObjectMapper objectMapper;

	public JacksonConverter(ObjectMapper objectMapper) {
		this.objectMapper = objectMapper;
	}

	public 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);
		}
	}

	public 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);
		}
	}

	private static class JsonTypedOutput implements TypedOutput {
		private final byte[] jsonBytes;
		private final String mimeType;

		public JsonTypedOutput(byte[] jsonBytes, String charset) {
			this.jsonBytes = jsonBytes;
			this.mimeType = "application/json; charset=" + charset;
		}

		public String fileName() {
			return null;
		}

		public String mimeType() {
			return mimeType;
		}

		public long length() {
			return jsonBytes.length;
		}

		public void writeTo(OutputStream out) throws IOException {
			out.write(jsonBytes);
		}
	}
}

实例Rest适配器

Rest适配器用于生成Rest interface service的源代码

    RestAdapter restAdapter = new RestAdapter.Builder().setServer("http://127.0.0.1:8080/rest").setConverter(converter).build();


建立Rest interface

   public interface TaskService{	
         @GET("/runtime/tasks")
         DataResponse getTasks();
   }

@GET:用的是Rest的get方法
DataRespone:是Rest的返回值


实例化interface并执行

   TaskService service=restAdapter.create(TaskService.class);
   DataResponse dataResponse= service.getTasks();

完整的例子

   public class Simple {
 	public static void main(String[] args) throws Exception {
		// Create our Converter
		JacksonConverter converter = new JacksonConverter(new ObjectMapper());

		RestAdapter restAdapter = new RestAdapter.Builder()
				.setServer("http://localhost:8080/").setConverter(converter)
				.build();

		
                               TaskService service=restAdapter.create(TaskService.class);
                               DataResponse dataResponse= service.getTasks();                
   } }

登陆(Activiti)

   String header="Basic " + Base64.encodeToString("kermit:kermit")
   @GET("/runtime/tasks")
   DataResponse getTasks(@Header("Authorization") String header);


你可能感兴趣的:(core,JAVA,工作流,J2EE,android,Retrofit)