导入依赖
compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
package com.bwie.pr;
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;
}
}
package com.bwie.pr;
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);
}
package com.bwie.pr;
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) {
}
});
}
});
}
}