Android Retrofit2.0网络加载框架
2018-01-01 18:51
483 查看
Android之Retrofit网络加载框架
本人对Retrofit2.0的话,也不是切入很深,今天就给大家上一个Demo案例,相信小伙伴们可以看明白
一:使用Retrofit的话需要导入依赖
二:先写一个接口,注解写上请求方式(RetrofitServices)
三:封装的工具类(RetrofitUtil)
四:在Activity里面请求
本人对Retrofit2.0的话,也不是切入很深,今天就给大家上一个Demo案例,相信小伙伴们可以看明白
一:使用Retrofit的话需要导入依赖
compile 'com.squareup.retrofit2:retrofit:2.0.0' compile 'com.squareup.retrofit2:converter-gson:2.0.2'
二:先写一个接口,注解写上请求方式(RetrofitServices)
import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Query; /** * author:Created by ZhangPengFei. * data: 2018/1/1 */ public interface RetrofitServices { /** * 请求方式后面的括号里写上固定网址后面的东西 * http://120.27.23.105/user/login * http://120.27.23.105/是每个接口固定的话 * user/login是拼接的登录 */ @GET("user/login") //固定网址后拼接的短域名 /** * http://120.27.23.105/user/login?mobile=xxx&password=xxx * mobile和password是参数 * username和userpass是值 */ Call<LoginBean> getLoginMsg(@Query("mobile") String username, @Query("password") String userpass); }
三:封装的工具类(RetrofitUtil)
import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; /** * author:Created by ZhangPengFei. * data: 2018/1/1 * Retrofit封装的工具类 */ public class RetrofitUtil { private static volatile RetrofitUtil instance; private final Retrofit retrofit; private RetrofitUtil(String baseurl) { retrofit = new Retrofit.Builder() .baseUrl(baseurl) .addConverterFactory(GsonConverterFactory.create()) .build(); } public static RetrofitUtil getInstance(String baseurl) { if (instance == null) { synchronized (RetrofitUtil.class) { if (instance == null) { instance = new RetrofitUtil(baseurl); } } } return instance; } public Retrofit getretrofit(){ return retrofit; } }
四:在Activity里面请求
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; public class MainActivity extends AppCompatActivity { private Retrofit retrofit; private EditText edName; private EditText edPass; private Button btnLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edName = (EditText) findViewById(R.id.username); edPass = (EditText) findViewById(R.id.userpass); btnLogin = (Button) findViewById(R.id.btnLogin); /** * 按钮的点击事件 * 点击登录 */ btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { /** * 不使用封装的工具类 */ /*retrofit = new Retrofit.Builder() .baseUrl("http://120.27.23.105/") .addConverterFactory(GsonConverterFactory.create()) .build();*/ /** * 使用封装的工具类 */ Retrofit getretrofit = RetrofitUtil.getInstance("http://120.27.23.105/").getretrofit(); /*** * 创建Bean类 */ RetrofitServices retrofitServices = getretrofit.create(RetrofitServices.class); Call<LoginBean> loginMsg = retrofitServices.getLoginMsg(edName.getText().toString(), edPass.getText().toString()); loginMsg.enqueue(new Callback<LoginBean>() { @Override public void onResponse(Call<LoginBean> call, Response<LoginBean> response) { /** * 直接获得请求的数据 */ String msg = response.body().getMsg(); Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); } @Override public void onFailure(Call<LoginBean> call, Throwable t) { } }); } }); } }
相关文章推荐
- Android 网络请求框架Retrofit2.0使用笔记
- Retrofit2.0+RxJava+RxAndroid——强强联合的网络请求框架
- Android 开源网络框架(Android-Async-Http、Volley、OkHttp3、Retrofit2.0)对比
- Android 开源网络框架(Android-Async-Http、Volley、OkHttp3、Retrofit2.0)对比
- Android 网络框架 Retrofit2.0介绍、使用和封装
- android快捷开发之Retrofit网络加载框架的简单使用
- Android/Java网络加载框架Retrofit(一)初识
- Retrofit 2.0使用详解,配合OkHttp、Gson,Android最强网络请求框架
- Android 网络框架 Retrofit2.0介绍、使用和封装
- Android/Java网络加载框架Retrofit(二)load bitmap into ListView
- Retrofit2.0+RxJava+RxAndroid——强强联合的网络请求框架
- Android中网络框架Retrofit2.0简单使用
- Android/Java网络加载框架Retrofit(三)结合RxJava2与RxAndroid2
- Android 网络请求框架 Retrofit2.0实践使用总结
- Android RxJava+Retrofit+okhttp异步网络加载框架
- Android RxJava+Retrofit异步网络加载框架
- Retrofit 2.0使用详解,配合OkHttp、Gson,Android最强网络请求框架
- Retrofit2.0+RxJava+RxAndroid——强强联合的网络请求框架
- Android 你必须了解的网络框架Retrofit2.0
- android快捷开发之Retrofit网络加载框架的简单使用