您的位置:首页 > 理论基础 > 计算机网络

Android Retrofit2.0网络加载框架

2018-01-01 18:51 483 查看
Android之Retrofit网络加载框架
本人对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) {

}
});
}
});

}
}




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