Retrofit2.0的get post请求简单数据
2017-10-30 16:21
399 查看
首先导入依赖
新建工程,创建Api的class类
}
在MainActivity类里面声明
这个方法是onCreate()外面的
参考的是另一篇博客,写的很详细 http://blog.csdn.net/android_study_ok/article/details/51480382
compile 'com.squareup.retrofit2:retrofit:2.0.2' compile 'com.squareup.retrofit2:converter-gson:2.0.2' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
新建工程,创建Api的class类
public class Api {
//http://v.juhe.cn/------这个在Api里面声明 // toutiao/index?type=shehui&key=6535f9a621cb3661b96f538ea740c9b6------这个在接口里面声明
public static final String BASE_URL = "http://v.juhe.cn/"; } 创建一个接口
public interface InterfaceApi { // toutiao/index?type=shehui&key=6535f9a621cb3661b96f538ea740c9b6------这个在接口里面声明 @GET("toutiao/index?type=shehui&key=6535f9a621cb3661b96f538ea740c9b6") //UserBean是自己创建bean类的名称,没有其他意思 Call<UserBean> getData();//getData()名字可以随便改!
//添加条件的get @GET("user/getUserInfo") Call<Bean> getData(@Query("uid") int uid); //post请求---getpost是方法名,不要忘记在MainActivity类里面改 @FormUrlEncoded @POST("user/getUserInfo") Call<Bean> getPost(@Field("uid") int uid);
}
在MainActivity类里面声明
private InterfaceApi interfaceApi; private Retrofit retrofit; private Call<UserBean> context;在onCreate()方法里面写
retrofit = new Retrofit.Builder().baseUrl(Api.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); //创建接口类 interfaceApi = retrofit.create(InterfaceApi.class); context = interfaceApi.getData(); context.enqueue(new Callback<UserBean>() { @Override public void onResponse(Call<UserBean> call, Response<UserBean> response) { if (response != null && response.isSuccessful() && response.body() != null) { try { UserBean string = response.body(); List<UserBean.ResultBean.DataBean> data = string.getResult().getData(); System.out.println("请求的data的长度 = " + data.size()); for (int i = 0; i < data.size(); i++) { System.out.println("标题是 = " + data.get(i).getTitle()); } } catch (Exception e) { e.printStackTrace(); } } } @Override public void onFailure(Call<UserBean> call, Throwable t) { } });
这个方法是onCreate()外面的
@Override protected void onDestroy() { super.onDestroy(); context.cancel(); }
参考的是另一篇博客,写的很详细 http://blog.csdn.net/android_study_ok/article/details/51480382
相关文章推荐
- Retrofit2.0的get请求和post请求(入门级)
- Nohttp网络请求数据,Post以及Get的简单实用以及设置缓存文字的的请求
- okhttp框架的最简单的基本使用 post和get请求以及用gson解析请求的数据
- Retrofit简单封装使用--Post请求封装请求体、Get请求通过参数、map方式构建url
- Retrofit 2.0关于get和post的网络访问的简单使用
- Nohttp网络请求数据,Post以及Get的简单实用以及设置缓存文字的的请求
- [Android] Retrofit2.0 GET和POST方式请求
- 初学者---Android (简单总结)Retrofit请求方式之get,post
- Retrofit的Get与Post请求
- AFNetwork数据请求之POST与GET方式 AFHTTPRequestOperationManager
- C# winform端 通过HttpWebRequest进行post和get请求,数据格式为json,后台java端接收,其中有关传输特殊字符(\t,\r,',\n,n)等处理
- Retrofit2.0 简单实现的网络请求
- 【Swift】 GET&POST请求 网络缓存的简单处理
- HTML中Get和Post请求的简单了解
- iOS 数据请求 同步get 同步post 异步get 异步post
- PHP之文件操作,http请求数据格式,模拟get和post,CURL模拟请求的使用
- retrofit的 get post 请求 + log日志
- Retrofit+Rxjava简单的请求网络数据
- Angularjs $http 请求数据(get.post)
- 浅谈使用Fiddler工具发送post请求(带有json数据)以及get请求(Header方式传参)