Retrofit 和 http_ok 获取ip

package com.cardvalue.sys.mode;

import android.content.Context;

import com.cardvalue.sys.activity.LoginActivity;
import com.cardvalue.sys.cache.LocalCache;
import com.cardvalue.sys.entity.Address;
import com.cardvalue.sys.entity.Auth;
import com.cardvalue.sys.entity.CheckMobileRegisterAuth;
import com.cardvalue.sys.entity.Login;
import com.cardvalue.sys.entity.PostCreateAuth;
import com.cardvalue.sys.entity.User;
import com.cardvalue.sys.entity.VerifyImgCode;

import org.json.JSONObject;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Path;
import retrofit2.http.Query;

/**
 * Created by cardvalue on 2016/4/8.
 */
public class Service {

    /**
     * <p>获取地理位置</p>
     */
    public  interface  GetAddress{
        @Headers({
                "X-CRM-Application-Id: "+"android",
                "X-CRM-Version: " + "2.0.1",
                "Content-Type: application/json"
        })
        @GET("new/m/getMultiLngAndLat?address")
        Call<Address>  attemAddress(@Query("address") String address);

    }


    /**
     * <p>修改用户</p>
     */
    public interface UpdateInfo{
        @Headers({
                "X-CRM-Application-Id: "+"android",
                "X-CRM-Version: " + "2.0.1",
                "Content-Type: application/json"
        })
        @PUT("merchants/{userId}")
        Call<User> attempUpdateInfo (@Header("X-CRM-Merchant-Id") String objectId,
                                      @Header("X-CRM-Access-Token") String accessToken,
                                      @Path("userId") String userId,
                                      @Body User updataInfo);
    }

    /**
     * <P>获取用户</P>
     */
    public   interface  getUserInfo{
        @Headers({
                "X-CRM-Application-Id: "+"android",
                "X-CRM-Version: " + "2.0.1",
                "Content-Type: application/json"
        })
        @GET("merchants/{userId}")
        Call<User> attempGetUserInfo (@Header("X-CRM-Merchant-Id") String objectId,
                                          @Header("X-CRM-Access-Token") String accessToken,
                                          @Path("userId") String userId);
    }


    /***
     * <p>修改密码</p>
     */
    public  interface  UpdatePassword{
        @Headers({
                "X-CRM-Application-Id: "+"android",
                "X-CRM-Version: " + "2.0.1",
                "Content-Type: application/json"
        })
        @PUT("merchants/{userId}/updatePassword")
        Call<Login> attempUpdatePassword (@Header("X-CRM-Merchant-Id") String objectId,
                                          @Header("X-CRM-Access-Token") String accessToken,
                                          @Path("userId") String userId,
                                        @Body Login updataPwd);

    }

    /**
     * <p>登录的接口</p>
     */
    public interface  LoginService{
        @Headers({
                "X-CRM-Application-Id: " +"android",
                "X-CRM-Version: " + "2.0.1"
        })
        @GET("login")
        Call<Login> attemptLogin(@Query("mobilePhone") String mobilePhone,@Query("password") String password);
    }



    /**
     * <p>检查手机号是否已注册授权</p>
     */
    public  interface  CheckMobile{
        @Headers({
                "X-CRM-Application-Id: " +"android",
                "X-CRM-Version: " + "2.0.1"
        })
        @GET("checkMobilePhoneRegisterAuth/{mobilePhone}")
        Call<CheckMobileRegisterAuth> attemptCheckMobile(@Path("mobilePhone") String mobilePhone);
    }




    /**
     * <P>创建授权   同意注册授权</P>
     */
    public interface CreateAuthorization{
        @Headers({
                "X-CRM-Application-Id: " +"android",
                "X-CRM-Version: " + "2.0.1",
                "Content-Type: application/json"
        })
        @POST("authorizations")
        Call<PostCreateAuth>  attemptCreateAuthorization (@Body PostCreateAuth task);
    }

    /**
     * <P>获取图形验验证码   获取手机验证码</P>
     */
    public interface GetImgCode{
        @Headers({
                "X-CRM-Application-Id: " +"android",
                "X-CRM-Version: " + "2.0.1",
                "Content-Type: application/json"
        })
        @POST("mobilePhoneVerifyCode")
        Call<VerifyImgCode> attemptVerifyImgCode(@Body VerifyImgCode code);
    }


    /**
     * <p>注册</p>
     */
    public interface Register{
        @Headers({
                "X-CRM-Application-Id: " +"android",
                "X-CRM-Version: " + "2.0.1",
                "Content-Type: application/json"
        })
        @POST("merchantsByMobilePhone")
        Call<Login> attempRegister (@Body Login login);
    }


}
<pre name="code" class="java">package com.cardvalue.sys.tool;

import android.content.Context;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * Created by cardvalue on 2016/4/15.
 */
public class Ip {
    private Retrofit retrofit;
    private  Context context;
    private Request request;
    OkHttpClient client=new OkHttpClient();
    public Ip(Context context){
        this.context=context;
         request=new Request.Builder()
                .url("http://ip.cn/")
                .build();
    }


   public void Ip(){
       retrofit = new Retrofit.Builder()
               .baseUrl("http://ip.cn/")
               .client(new OkHttpClient())
               .addConverterFactory(new Converter.Factory() {
                   @Override
                   public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
                       return new Converter<ResponseBody, String>() {
                           @Override
                           public String convert(ResponseBody value) throws IOException {
                               Utiltools.printE("==IP=",value.string()+"");
                               return value.string();
                           }
                       };
                   }
               })
               .build();
   }


    public void IpService(){
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String respo=response.body().string();
                String ip= respo.substring(respo.indexOf("<code>") +6, respo.indexOf("</code>"));
                Utiltools.printE("=====","======"+ip);
            }
        });
    }
}


 
 

你可能感兴趣的:(retrofit)