天天看点

Retrofit网络请求

Retrofit

  一、  特点

  1. 性能最好,处理最快
  2. 使用REST API时非常方便;
  3. 传输层默认就使用OkHttp;
  4. 支持NIO;
  5. 拥有出色的API文档和社区支持
  6. 速度上比volley更快;
  7. 如果你的应用程序中集成了OKHttp,Retrofit默认会使用OKHttp处理其他网络层请求。
  8. 默认使用Gson

二、使用

Retrofit支持同步和异步两种方式,在使用时,需要将请求地址转换为接口,通过注解来指定请求方法,请求参数,请求头,返回值等信息。还是使用之前的person的那段json值,get请求到服务器后从数据库查询数据,返回值为查询到的数据,post请求向服务器提交一条数据,返回值为提交的数据。 

第一步:gradle文件中进行配置

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit2:converter-scalars:2.0.0'
           

第二步:定义一个接口,接口中使用注解注明请求方式,及对应的请求路径

public interface ProjectAPI {

    @GET
    Call<String> getMethod(@Url String url);

    @FormUrlEncoded
    @POST
    Call<String> postMethod(@Url String url, @FieldMap Map<String,String> map);

}
           

Get和Post请求方式的封装,参数通过map集合的方式进行传递

public class HttpManger {

    /**
     * @param baseUrl  基础Url
     * @param url       附加Url
     * @param callback  添加请求回调,这里直接使用的是Retrofit自身的回调接口
     */
    public static void getMethod(String baseUrl, String url, final Callback<String> callback) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(ScalarsConverterFactory.create()).build();

        ProjectAPI projectAPI = retrofit.create(ProjectAPI.class);

        Call<String> call = projectAPI.getMethod(url);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                //调用回调
                callback.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                //调用回调
                callback.onFailure(call, t);
            }
        });
    }

    public static void postMethod(String baseUrl, String url, Map<String, String> map, final Callback<String> callback) {
        
        Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl).client(new OkHttpClient()).addConverterFactory(ScalarsConverterFactory.create()).build();

        ProjectAPI projectAPI = retrofit.create(ProjectAPI.class);

        Call<String> call = projectAPI.postMethod(url, map);

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                callback.onResponse(call, response);
            }
            @Override
            public void onFailure(Call<String> call, Throwable t) {
                callback.onFailure(call, t);
            }
        });
    }
}
           

第二步:方法调用

Get方式

HttpManger.getMethod("http://blog.csdn.net/qq_39735504", "kefulist", new Callback<String>() {
    @Override
    public void onResponse(Call<String> call, Response<String> response) {
        Toast.makeText(MainActivity.this, "A-"+response.body(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailure(Call<String> call, Throwable t) {
        Toast.makeText(MainActivity.this, "B-"+t.getMessage(), Toast.LENGTH_SHORT).show();
    }
});
           

Post方式

Map<String, String> map=new HashMap<>();
map.put("userid","12345678911");
map.put("pwd","123456");
HttpManger.postMethod("http://blog.csdn.net/qq_39735504", "login",map,new Callback<String>(){
    @Override
    public void onResponse(Call<String> call, Response<String> response) {
        Toast.makeText(MainActivity.this, "A-"+response.body(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailure(Call<String> call, Throwable t) {
        Toast.makeText(MainActivity.this, "B-"+t.getMessage(), Toast.LENGTH_SHORT).show();
    }
});
           

Retrofit+RxJava网络请求Demo地址:https://download.csdn.net/download/qq_39735504/10274074

继续阅读