java网络请求

api

package com.rcplatform.athena.coin.api;


import com.rcplatform.athena.coin.models.data.GooglePlayOauthResponse;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

/**
 * Created by Yang Peng on 2017/5/11.
 */
public interface GooglePlayOauthApi {


    /**
     * grant_type=authorization_code
     * code=<the code from the previous step>
     * client_id=<the client ID token created in the APIs Console>
     * client_secret=<the client secret corresponding to the client ID>
     * redirect_uri=<the URI registered with the client ID>
     *
     * @return
     */
    @FormUrlEncoded
    @POST("https://accounts.google.com/o/oauth2/token")
    Call<GooglePlayOauthResponse> oauth(@Field("grant_type") String grantType, @Field("code") String code,
                                        @Field("client_id") String clientId, @Field("client_secret") String clientSecret,
                                        @Field("redirect_uri") String redirectUri);


    /**
     * Using the refresh token
     * <p>
     * Each access token is only valid for a short time. Once the current access token expires, the server will need to use the refresh token to get a new one. To do this, send a POST request to https://accounts.google.com/o/oauth2/token with the following fields set:
     * <p>
     * grant_type=refresh_token
     * client_id=<the client ID token created in the APIs Console>
     * client_secret=<the client secret corresponding to the client ID>
     * refresh_token=<the refresh token from the previous step>
     *
     * @param grantType
     * @param refreshToken
     * @param clientId
     * @param clientSecret
     * @return
     */
    @FormUrlEncoded
    @POST("https://accounts.google.com/o/oauth2/token")
    Call<GooglePlayOauthResponse> refreshToken(@Field("grant_type") String grantType, @Field("refresh_token") String refreshToken,
                                               @Field("client_id") String clientId, @Field("client_secret") String clientSecret);

}

请求类

public class GooglePlayPaymentService {

   private GooglePlayOauthApi googlePlayOauthApi;

   private GooglePlayPaymentApi googlePlayPaymentApi;

     public GooglePlayPaymentService(String baseUrl) {
        this.baseUrl = baseUrl;
        Retrofit retrofit = new RetrofitClient(baseUrl).getRetrofit();
        googlePlayPaymentApi = retrofit.create(GooglePlayPaymentApi.class);
        googlePlayOauthApi = retrofit.create(GooglePlayOauthApi.class);
    }

调用
  @Retryable(value = HttpErrorException.class, backoff = @Backoff(value = 10000))
    public GooglePlayProductResponse productVerify(String packageName, String productId, String token, String accessToken) throws TokenExpireException, HttpErrorException {
        logger.debug("发起google play 消耗购买验证");
        Call<GooglePlayProductResponse> call =
                googlePlayPaymentApi.getProduct(packageName, productId, token, accessToken);
        Response<GooglePlayProductResponse> response;
        try {
            response = call.execute();
            if (response.code() == 401) {
                logger.error("当前token expired");
                throw new TokenExpireException();
            }
        } catch (IOException e) {
            logger.error("we have some trouble packageName{},productId{},token{},accessToken{}", packageName, productId, token, accessToken);
            throw new HttpErrorException(e.getMessage());
        }
        return response.body();
    }

 private <T> T getResponse(Call<T> t) throws TokenExpireException, HttpErrorException {
        try {
            Response<T> response = t.execute();
            if (response.code() == 401) {
                throw new TokenExpireException();
            }
            return response.body();
        } catch (IOException e) {
            throw new HttpErrorException();
        }
    }

注入

@Configuration
@EnableConfigurationProperties(PaymentProperties.class)
public class PaymentAutoConfiguration {


	private PaymentProperties paymentProperties;


	public PaymentAutoConfiguration(PaymentProperties paymentProperties) {
		this.paymentProperties = paymentProperties;
	}


	@Bean
	@Primary
	public AppStorePaymentService appStorePaymentService() {
		return new AppStorePaymentService(paymentProperties.getAppStorePassword(), paymentProperties.getAppStoreUrl());
	}


	@Bean
	@ConditionalOnMissingBean
	public GooglePlayPaymentService googlePlayPaymentService() {
		return new GooglePlayPaymentService(paymentProperties.getGooglePlayUrl());
	}


	@Bean
	public AppStorePaymentService appStoreSandboxPaymentService() {
		return new AppStorePaymentService(paymentProperties.getAppStoreSandboxUrl());
	}

}

retrofit

  restful的http网络请求框架,本质okhttp完成,仅负责网络请求接口的封装

  https://www.jianshu.com/p/865e9ae667a0

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值