Retrofit http客户端

除了gson转换器,还有其他的支持json的转换器,或者自定义转换器(不一定要转换对象,而是有时需要对http头中的错误码进行处理)

		<dependency>
			<groupId>com.squareup.retrofit2</groupId>
			<artifactId>retrofit</artifactId>
			<version>2.3.0</version>
		</dependency>
		<dependency>
			<groupId>com.squareup.retrofit2</groupId>
			<artifactId>converter-gson</artifactId>
			<version>2.3.0</version>
		</dependency>

package gaofeng.http.retrofit;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface GitHubService {
	@GET("users/{user}/repos")
	Call<List<Repo>> listRepos(@Path("user") String user);
}
package gaofeng.http.retrofit;

public class Repo {

	private int id;
	private String name;
	private String full_name;
	
	@Override
	public String toString() {
		return "Repo [id=" + id + ", name=" + name + ", full_name=" + full_name + "]";
	}
}

package gaofeng.http.retrofit;

import java.io.IOException;
import java.util.List;

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

public class Retrofit1 {

	public static void main(String[] args) throws IOException {
		Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/")
				.addConverterFactory(GsonConverterFactory.create()).build();

		GitHubService service = retrofit.create(GitHubService.class);
		Call<List<Repo>> call = service.listRepos("ggaofengg");
		
		//同步请求
		// Response<List<Repo>> re = call.execute();
		// System.err.println(re.body().get(0));

		//异步请求
		call.enqueue(new Callback<List<Repo>>() {
			public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
				System.err.println(response.code() +" "+ response.message());
				
				System.err.println(response.body().size());
				System.err.println(response.body().get(0));		
			}

			public void onFailure(Call<List<Repo>> call, Throwable t) {
				t.printStackTrace();
			}
		});
		System.err.println("hehe");
	}
}

Retrofit2.1学习:Call接口的使用

http://www.jianshu.com/p/05bbf3aa2269



public class Test {

	public static void main(String[] args) throws IOException {
		Retrofit retrofit = new Retrofit.Builder().baseUrl("http://127.0.0.1:5000")
				.build();//不要转换器,用原始ResponseBody,当返回500错误时,也可以通过errorbody获取响应内容

		RestfulClient s = retrofit.create(RestfulClient.class);
		Call<ResponseBody> d = s.listRepos("999");
		Response<ResponseBody> p= d.execute();
		 
		System.out.println(p.code()); 
		System.out.println(p.message());
//		System.out.println(p.errorBody().string());
		System.out.println(p.body().string());
	}
	public static void main2(String[] args) throws IOException {
		Retrofit retrofit = new Retrofit.Builder().baseUrl("http://127.0.0.1:5000")
				.addConverterFactory(new Factory() {//自定义转换器
					@Override
					public Converter<ResponseBody, ?> responseBodyConverter(Type type,
					        Annotation[] annotations, Retrofit retrofit) {
						
					      return new Converter<ResponseBody, String>() {
			                    public String convert(ResponseBody value) throws IOException {
			                        return "value.string()";
			                    }
					      };
					    }
				}).build();
		RestfulClient2 s = retrofit.create(RestfulClient2.class);
		Call<String> d = s.listRepos("999");
		Response<String> p= d.execute();
		 
		System.out.println(p.code()); 
		System.out.println(p.message());
		System.out.println(p.errorBody());
//		System.out.println(p.body());
	}
}
interface RestfulClient {
	@GET("/app/")
	Call<ResponseBody> listRepos(@Query("user") String user);
}
interface RestfulClient2 {
	@GET("/app/")
	Call<String> listRepos(@Query("user") String user);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值