我的第一个Retrofit小demo
2016-05-01 17:06
585 查看
先看下项目结构
MainActivity
package com.cisetech.myretrofitdemo; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TvService tvService = RetrofitWrapper.getInstance().create(TvService.class); Call<TvInfo> call = tvService.getTvInfoList( "1", "8a7204eb63ba0bab009b338025f42df2" ); call.enqueue( new Callback<TvInfo>() { @Override public void onResponse(Call<TvInfo> call, Response<TvInfo> response) { TvInfo tv = response.body(); //这里显示了列表中的第一条的内容,所以get(0) Toast.makeText( MainActivity.this, tv.getResult().get(0).getChannelName(), Toast.LENGTH_LONG ).show(); Log.i(TAG,response.body().toString()); } @Override public void onFailure(Call<TvInfo> call, Throwable t) { Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show(); } } ); } }
RetrofitWrapper
package com.cisetech.myretrofitdemo; import android.content.Context; import retrofit2.GsonConverterFactory; import retrofit2.Retrofit; /** * Created by Jaycee on 16/4/19. * E-mail:jayceeok@foxmail.com */ public class RetrofitWrapper { private static RetrofitWrapper instance; private Context mContext; private Retrofit retrofit; private RetrofitWrapper() { retrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL).addConverterFactory( GsonConverterFactory.create()).build(); } public static RetrofitWrapper getInstance() { if (instance == null) { synchronized (RetrofitWrapper.class) { instance = new RetrofitWrapper(); } } return instance; } public <T> T create(Class<T> service) { return retrofit.create(service); } public class Constant { public static final String BASE_URL = "http://japi.juhe.cn"; } }
TvInfo
package com.cisetech.myretrofitdemo; import java.util.List; /** * Created by Jaycee on 16/4/19. * E-mail:jayceeok@foxmail.com */ public class TvInfo { /** * error_code : 0 * reason : Success! * result : [{"channelName":"CCTV-1 综合","pId":1,"rel":"cctv1","url":"http://tv.cntv.cn/live/cctv1"},{"channelName":"CCTV-2 财经","pId":1,"rel":"cctv2","url":"http://tv.cntv.cn/live/cctv2"},{"channelName":"CCTV-3 综艺","pId":1,"rel":"cctv3","url":"http://tv.cntv.cn/live/cctv3"},{"channelName":"CCTV-4 (亚洲)","pId":1,"rel":"cctv4","url":"http://tv.cntv.cn/live/cctv4"},{"channelName":"CCTV-4 (欧洲)","pId":1,"rel":"cctveurope","url":"http://tv.cntv.cn/live/cctveurope"},{"channelName":"CCTV-4 (美洲)","pId":1,"rel":"cctvamerica","url":"http://tv.cntv.cn/live/cctvamerica"},{"channelName":"CCTV-5 体育","pId":1,"rel":"cctv5","url":"http://tv.cntv.cn/live/cctv5"},{"channelName":"CCTV-6 电影","pId":1,"rel":"cctv6","url":"http://tv.cntv.cn/live/cctv6"},{"channelName":"CCTV-7 军事农业","pId":1,"rel":"cctv7","url":"http://tv.cntv.cn/live/cctv7"},{"channelName":"CCTV-8 电视剧","pId":1,"rel":"cctv8","url":"http://tv.cntv.cn/live/cctv8"},{"channelName":"CCTV-9 纪录","pId":1,"rel":"cctvjilu","url":"http://tv.cntv.cn/live/cctvjilu"},{"channelName":"CCTV-9 纪录(英)","pId":1,"rel":"cctvdoc","url":"http://tv.cntv.cn/live/cctvdoc"},{"channelName":"CCTV-10 科教","pId":1,"rel":"cctv10","url":"http://tv.cntv.cn/live/cctv10"},{"channelName":"CCTV-11 戏曲","pId":1,"rel":"cctv11","url":"http://tv.cntv.cn/live/cctv11"},{"channelName":"CCTV-12 社会与法","pId":1,"rel":"cctv12","url":"http://tv.cntv.cn/live/cctv12"},{"channelName":"CCTV-13 新闻","pId":1,"rel":"cctv13","url":"http://tv.cntv.cn/live/cctv13"},{"channelName":"CCTV-14 少儿","pId":1,"rel":"cctvchild","url":"http://tv.cntv.cn/live/cctvchild"},{"channelName":"CCTV-15 音乐","pId":1,"rel":"cctv15","url":"http://tv.cntv.cn/live/cctv15"},{"channelName":"CCTV-NEWS","pId":1,"rel":"cctv9","url":"http://tv.cntv.cn/live/cctv9"},{"channelName":"CCTV-Français","pId":1,"rel":"cctvfrench","url":"http://tv.cntv.cn/live/cctvfrench"},{"channelName":"CCTV-Español","pId":1,"rel":"cctvxiyu","url":"http://tv.cntv.cn/live/cctvxiyu"},{"channelName":"CCTV-العربية","pId":1,"rel":"cctvarabic","url":"http://tv.cntv.cn/live/cctvarabic"},{"channelName":"CCTV-Русский","pId":1,"rel":"cctvrussian","url":"http://tv.cntv.cn/live/cctvrussian"},{"channelName":"CCTV体育赛事","pId":1,"rel":"cctv5plus","url":"http://tv.cntv.cn/live/cctv5plus"}] */ private int error_code; private String reason; /** * channelName : CCTV-1 综合 * pId : 1 * rel : cctv1 * url : http://tv.cntv.cn/live/cctv1 */ private List<ResultEntity> result; public int getError_code() { return error_code;} public void setError_code(int error_code) { this.error_code = error_code;} public String getReason() { return reason;} public void setReason(String reason) { this.reason = reason;} public List<ResultEntity> getResult() { return result;} public void setResult(List<ResultEntity> result) { this.result = result;} public static class ResultEntity { private String channelName; private int pId; private String rel; private String url; public String getChannelName() { return channelName;} public void setChannelName(String channelName) { this.channelName = channelName;} public int getPId() { return pId;} public void setPId(int pId) { this.pId = pId;} public String getRel() { return rel;} public void setRel(String rel) { this.rel = rel;} public String getUrl() { return url;} public void setUrl(String url) { this.url = url;} }}
TvService
package com.cisetech.myretrofitdemo; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Query; /** * Created by Jaycee on 16/4/19. * E-mail:jayceeok@foxmail.com * http://japi.juhe.cn */ public interface TvService { @GET("/tv/getChannel") Call<TvInfo> getTvInfoList(@Query("pId") String pId, @Query("key") String key); }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories