标签:
interface SimpleGET{ @GET("/") //表明是GET方式. "/"会拼接在setEndpoint(url)中url(主机地址)的后面. Response getResponse(); //可以简单的理解为网络访问完把Response转换为一个对象.这里没有转换,还是Response. }
String url="http://tieba.baidu.com"; RestAdapter adapter=new RestAdapter.Builder().setEndpoint(url).build(); //setEndpoint(url)设置网络访问地址的主机地址 SimpleGET create = adapter.create(SimpleGET.class); //retrofit会生成上面的SimpleGET接口的一个的实例. Response response = create.getResponse(); int status = response.getStatus(); //返回码 try { InputStream in = response.getBody().in(); //response的输入流.看到网络访问的输入流都很熟悉吧.想转换成字符串还是json还是图片就看服务器给的是什么了. } catch (IOException e) { e.printStackTrace(); }
interface SimplePOST{ @POST("/android") //表明是使用Post方式进行访问. 并且会把后面的路径拼接到主机名的后面-->拼接到setEndpoint(url)中的url的后面. Response getResponse(); }
String url="http://tieba.baidu.com"; RestAdapter adapter=new RestAdapter.Builder().setEndpoint(url).build(); //和上面post后面的路径拼接,想当于访问http://tieba.baidu.com/android SimplePOST create = adapter.create(SimplePOST.class); Response response = create.getResponse(); int status = response.getStatus(); try { InputStream in = response.getBody().in(); } catch (IOException e) { e.printStackTrace(); }
interface GitHub { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> contributors(@Path("owner") String owner, @Path("repo") String repo);
private static final String API_URL = "https://api.github.com"; RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(API_URL).build(); GitHub github = restAdapter.create(GitHub.class); List<Contributor> contributors = github.contributors("square", "retrofit"); //相当于对路径中{}部分进行动态替换. 相当于访问https://api.github.com/repos/square/retrofit/contributors
static class Contributor { //一个pojo类(Plain Ordinary Java Object)简单的Java对象-->相比javaBean更简单. String login; int contributions; } interface GitHub { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> contributors(@Path("owner") String owner, @Path("repo") String repo); }
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(API_URL).build(); //默认设置了GsonConverter的转换器.把response直接解析为一个POJO对象. GitHub github = restAdapter.create(GitHub.class); //访问这个地址返回的是一个JsonArray,JsonArray的每一个元素都有login和contributions这2个key和其对应的value.提取出来封装进POJO对象中. List<Contributor> contributors = github.contributors("square", "retrofit"); for (Contributor contributor : contributors) { Log.v("retrofit", contributor.login + " (" + contributor.contributions + ")"); }
class StringConverter implements Converter{ @Override public Object fromBody(TypedInput input, Type type) throws ConversionException { //response输入流到对象的转换, try { InputStream in = input.in(); //获取了输入流,就可以根据意愿随意转换了. String str = convertStream2String(in); return str; } catch (IOException e) { e.printStackTrace(); } return null; } private String convertStream2String(InputStream in) throws IOException { //inputStream转换为String BufferedReader reader=new BufferedReader(new InputStreamReader(in)); StringBuilder sb=new StringBuilder(); String line=null; while((line=reader.readLine())!=null){ sb.append(line); sb.append("\n"); } return sb.toString(); } @Override public TypedOutput toBody(Object obj) { //对象到输出流的转换 return null; } }
interface StringClient { @GET("/") String getString(); //方法的返回值是String,需要StringConverter转换器Converter把Response转换为String. }
String url="http://tieba.baidu.com"; RestAdapter adapter=new RestAdapter.Builder().setEndpoint(url).setConverter(new StringConverter()).build(); StringClient create = adapter.create(StringClient.class); String str = create.getString();
标签:
原文地址:http://www.cnblogs.com/laiqurufeng/p/4483658.html