Retrofit 网络请求框架

compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'


接口
package com.example.rxjava.utils;
import com.example.rxjava.GoodBeanns;
import java.util.Map;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.QueryMap;

/**
 * Created by Administrator on 2018/1/18 0018.
 */
public interface ApiService {
//http://120.27.23.105/user/login
    @GET("user/login")
    Call<GoodBeanns> getGoods(@QueryMap Map<String,String> map);
    //做一个bean    //map中是拼接在后面的
}
返回数据的接口


package com.example.rxjava.utils;

/**
 * Created by Administrator on 2018/1/18 0018.
 */

public interface CallBack {
    void onSuccess(Object o);
    void onFailed(Throwable t);
}
请求工具类
package com.example.rxjava.utils;

import com.example.rxjava.GoodBeanns;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2018/1/18 0018.
 */

public class RetrofitUtils {

    private static volatile RetrofitUtils instance;
    public static RetrofitUtils getInstance(){
        if (instance==null){
            synchronized (RetrofitUtils.class){
                if (instance==null){
                    instance=new RetrofitUtils();
                }
            }
        }
        return instance;
    }
    public RetrofitUtils getNews(String baseurl, Map map, final CallBack callBack){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseurl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        Call<GoodBeanns> call = apiService.getGoods(map);
        call.enqueue(new Callback<GoodBeanns>() {
            @Override
            public void onResponse(Call<GoodBeanns> call, Response<GoodBeanns> response) {
                GoodBeanns body = response.body();
                callBack.onSuccess(body);
            }
            @Override
            public void onFailure(Call<GoodBeanns> call, Throwable t) {
                callBack.onFailed(t);
            }
        });
        return null;
    }

}

如何使用
Map<String,String>  map=new HashMap<>();
map.put("mobile","13269671778");
map.put("password","1234567");
RetrofitUtils retrofitUtils=RetrofitUtils.getInstance().getNews("http://120.27.23.105", map, new CallBack() {
    @Override
    public void onSuccess(Object o) {
        GoodBeanns b= (GoodBeanns) o;
        Log.d("zzz",b.getMsg());
    }

    @Override
    public void onFailed(Throwable t) {

    }
});



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值