RetrofitUtils 联网工具类

MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        RetrofitUtils.getRetrofitUtils().init();
        findViewById(R.id.bt_banner).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RetrofitUtils.mRetrofitUtils.getCarPost(MainActivity.this);

            }
        });
    }
}

MainService 


public interface MainService {

    @GET("/product/getProductCatagory")
    Call<ResponseBody> getBanner();


    @GET("/product/getCarts")
    Call<ResponseBody> getCar(@Query("uid") int uid);

    @POST("/product/getCarts")
    Call<ResponseBody> getCarPost(@QueryMap Map<String,String> map);
}

RetrofitUtils


public class RetrofitUtils {
    private Retrofit mRetrofit;
    //私有的构造
    private RetrofitUtils(){}
    public static RetrofitUtils mRetrofitUtils;
    //单例模式
    public static RetrofitUtils getRetrofitUtils(){
        if (mRetrofitUtils==null){
            mRetrofitUtils=new RetrofitUtils();
        }
        return mRetrofitUtils;
    }
    //
    public void init(){
         mRetrofit = new Retrofit.Builder()
                .baseUrl("http://www.zhaoapi.cn/")
                .build();
    }
    public void getBanner(final Context context){
        mRetrofit.create(MainService.class).getBanner().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                try {
                    String data = response.body().string();
                    Toast.makeText(context, "a"+data, Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }
    public void getCarPost(final Context context){
        Map<String,String> map = new HashMap<>();
        map.put("uid","71");
        mRetrofit.create(MainService.class).getCarPost(map).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                try {
                    String data = response.body().string();
                    Toast.makeText(context, "a"+data, Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值