您的位置:首页 > 移动开发 > Android开发

我的第一个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);
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android studio