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=
     * client_id=
     * client_secret=
     * redirect_uri=
     *
     * @return
     */
    @FormUrlEncoded
    @POST("https://accounts.google.com/o/oauth2/token")
    Call 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
     * 

* 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: *

* grant_type=refresh_token * client_id= * client_secret= * refresh_token= * * @param grantType * @param refreshToken * @param clientId * @param clientSecret * @return */ @FormUrlEncoded @POST("https://accounts.google.com/o/oauth2/token") Call 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 call =
                googlePlayPaymentApi.getProduct(packageName, productId, token, accessToken);
        Response 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 getResponse(Call t) throws TokenExpireException, HttpErrorException {
        try {
            Response 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

 

你可能感兴趣的:(java网络请求)