zoukankan      html  css  js  c++  java
  • Api管家系列(一):初探

    前段时间发现一个很好用的API管理工具--API管家,用了一段时间,已经感觉离不开了,抱着分享使我快乐的想法,因为刚开始用的时候随便写过一篇简介,不是很详细,所以现在就重新写,把我这段时间使用的经验和大家一起讨论。

    进入正题:打开浏览器,输入www.apigj.com 来到首页,接下来自然是登录或注册了

    创建一个新账号,成功后会来到项目列表界面,会提示是否需要创建示例项目,点击确定后,就会创建一个新的项目出来

    点击进入示例项目,示例项目一共有1个文件夹和5个接口,可以看到没有经过测试,都在未测试状态

    点击某个接口,接口文档就显示出来了,有请求的URL,版本号,描述,请求方式,参数等等,左上角是对接口的操作按扭

     

    点击编辑,可以看到编辑接口的界面,注意我红色圈出的位置是个tab控件,可以点击切换编辑的内容

    把请求Class的openid直接删除,点击保存

     

    回到接口文档页面,可以看到openid已经从请求Class里消失了,这里还有个特别的功能,我用红色图出了,点击会生成mock参数

    点击左上角的生成按扭,这里就是Api管家最大特色了,可以直接生成代码

    点击生成代码,会出现语言的选项,一共9种语言,对我来说是足够用了

     举个栗子,我常用的Java(咖啡杯),又分3种引入包

    选择Gson

    请求和返回的Class文件就直接生成了,值的提一下的是ResSendSms继承于ResCommon,这个是在文档中编辑的,下次再展开细说编辑的功能

    上生成的代码,里面自动带了文档中的描述作为注释,实在是太方便了

     1 /***
     2  * @接口:SendSMS
     3  * @URL:host + /sendsms
     4  * @编码:www.apigj.com
     5  * @版本号:1.1
     6  ***/
     7 
     8 import java.io.IOException;
     9 import java.util.ArrayList;
    10 import java.util.List;
    11 
    12 import com.google.gson.TypeAdapter;
    13 import com.google.gson.annotations.JsonAdapter;
    14 import com.google.gson.annotations.SerializedName;
    15 import com.google.gson.stream.JsonReader;
    16 import com.google.gson.stream.JsonWriter;
    17 
    18 @JsonAdapter(ReqSendSms.ReqSendSmsTypeAdapter.class)
    19 public class ReqSendSms {
    20     // 类型版本,用于查询类型是否已过期
    21     public static final int Version = 2;
    22 
    23     /***
    24      * 参数描述:用户手机号
    25      * 是否可为空:否
    26     ***/
    27     @SerializedName("mobile")
    28     private String mobile;
    29     public String getMobile(){
    30         return mobile;
    31     }
    32     public void setMobile(String mobile){
    33         this.mobile = mobile;
    34     }
    35 
    36     /***
    37      * 检查类型完整性
    38      *
    39      * @return true代表类型通过完整性检查 
    40     ***/
    41     public boolean checkVarRequire() {
    42         if(getMobile() == null){
    43             System.out.println("Lake of (mobile)");
    44             return false;
    45         }
    46         return true;
    47     }
    48 
    49     public static class ReqSendSmsTypeAdapter<T> extends TypeAdapter<ReqSendSms> {
    50         @Override
    51         public void write(JsonWriter out, ReqSendSms value) throws IOException {
    52             out.beginObject();
    53             writeOBJ(out, (ReqSendSms)value);
    54             out.endObject();
    55         }
    56 
    57         protected void writeOBJ(JsonWriter out, ReqSendSms value) throws IOException {
    58             out.name("mobile").value(value.getMobile());
    59         }
    60 
    61         @Override
    62         public ReqSendSms read(JsonReader in) throws IOException {
    63             ReqSendSms res = new ReqSendSms();
    64             in.beginObject();
    65             while (in.hasNext()) {
    66                 String propertyName = in.nextName();
    67                 if(!readOBJ(res, in, propertyName)) {
    68                     in.skipValue();
    69                 }
    70             }
    71             in.endObject();
    72             return res;
    73         }
    74 
    75         protected boolean readOBJ(ReqSendSms res, JsonReader in, String propertyName) throws IOException {
    76             if (propertyName.equals("mobile")) {
    77                 try {
    78                     res.setMobile(in.nextString());
    79                 } catch(IllegalStateException e) {in.skipValue();}
    80                 return true;
    81             }
    82             return false;
    83         }
    84 
    85     }
    86 
    87 }
    请求类 ReqSendSms.java
      1 /***
      2  * @接口:SendSMS
      3  * @URL:host + /sendsms
      4  * @编码:www.apigj.com
      5  * @版本号:1.1
      6  ***/
      7 
      8 import java.io.IOException;
      9 import java.util.ArrayList;
     10 import java.util.List;
     11 
     12 import com.google.gson.TypeAdapter;
     13 import com.google.gson.annotations.JsonAdapter;
     14 import com.google.gson.annotations.SerializedName;
     15 import com.google.gson.stream.JsonReader;
     16 import com.google.gson.stream.JsonWriter;
     17 
     18 @JsonAdapter(ResCommon.ResCommonTypeAdapter.class)
     19 public class ResCommon {
     20     // 类型版本,用于查询类型是否已过期
     21     public static final int Version = 1;
     22 
     23     /***
     24      * 参数描述:返回码
     25      * 是否可为空:否
     26     ***/
     27     @SerializedName("code")
     28     private Integer code;
     29     public Integer getCode(){
     30         return code;
     31     }
     32     public void setCode(Integer code){
     33         this.code = code;
     34     }
     35 
     36     /***
     37      * 参数描述:返回提示
     38      * 是否可为空:是
     39     ***/
     40     @SerializedName("msg")
     41     private String msg;
     42     public String getMsg(){
     43         return msg;
     44     }
     45     public void setMsg(String msg){
     46         this.msg = msg;
     47     }
     48 
     49     /***
     50      * 检查类型完整性
     51      *
     52      * @return true代表类型通过完整性检查 
     53     ***/
     54     public boolean checkVarRequire() {
     55         if(getCode() == null){
     56             System.out.println("Lake of (code)");
     57             return false;
     58         }
     59         return true;
     60     }
     61 
     62     public static class ResCommonTypeAdapter<T> extends TypeAdapter<ResCommon> {
     63         @Override
     64         public void write(JsonWriter out, ResCommon value) throws IOException {
     65             out.beginObject();
     66             writeOBJ(out, (ResCommon)value);
     67             out.endObject();
     68         }
     69 
     70         protected void writeOBJ(JsonWriter out, ResCommon value) throws IOException {
     71             out.name("code").value(value.getCode());
     72             out.name("msg").value(value.getMsg());
     73         }
     74 
     75         @Override
     76         public ResCommon read(JsonReader in) throws IOException {
     77             ResCommon res = new ResCommon();
     78             in.beginObject();
     79             while (in.hasNext()) {
     80                 String propertyName = in.nextName();
     81                 if(!readOBJ(res, in, propertyName)) {
     82                     in.skipValue();
     83                 }
     84             }
     85             in.endObject();
     86             return res;
     87         }
     88 
     89         protected boolean readOBJ(ResCommon res, JsonReader in, String propertyName) throws IOException {
     90             if (propertyName.equals("code")) {
     91                 try {
     92                     res.setCode(in.nextInt());
     93                 } catch(IllegalStateException e) {in.skipValue();}
     94                 return true;
     95             }
     96             else if (propertyName.equals("msg")) {
     97                 try {
     98                     res.setMsg(in.nextString());
     99                 } catch(IllegalStateException e) {in.skipValue();}
    100                 return true;
    101             }
    102             return false;
    103         }
    104 
    105     }
    106 
    107 }
    返回类的父类 ResCommon.java
     1 /***
     2  * @接口:SendSMS
     3  * @URL:host + /sendsms
     4  * @编码:www.apigj.com
     5  * @版本号:1.1
     6  ***/
     7 
     8 import java.io.IOException;
     9 import java.util.ArrayList;
    10 import java.util.List;
    11 
    12 import com.google.gson.TypeAdapter;
    13 import com.google.gson.annotations.JsonAdapter;
    14 import com.google.gson.annotations.SerializedName;
    15 import com.google.gson.stream.JsonReader;
    16 import com.google.gson.stream.JsonWriter;
    17 
    18 @JsonAdapter(ResSendSms.ResSendSmsTypeAdapter.class)
    19 public class ResSendSms extends ResCommon {
    20     // 类型版本,用于查询类型是否已过期
    21     public static final int Version = 2;
    22 
    23     /***
    24      * 参数描述:事件ID,记录接收短信的用户
    25      * 是否可为空:否
    26     ***/
    27     @SerializedName("eventid")
    28     private String eventid;
    29     public String getEventid(){
    30         return eventid;
    31     }
    32     public void setEventid(String eventid){
    33         this.eventid = eventid;
    34     }
    35 
    36     /***
    37      * 检查类型完整性
    38      *
    39      * @return true代表类型通过完整性检查 
    40     ***/
    41     public boolean checkVarRequire() {
    42         if(!super.checkVarRequire()){
    43             return false;
    44         }
    45         if(getEventid() == null){
    46             System.out.println("Lake of (eventid)");
    47             return false;
    48         }
    49         return true;
    50     }
    51 
    52     public static class ResSendSmsTypeAdapter<T> extends ResCommonTypeAdapter<ResSendSms> {
    53         @Override
    54         public void write(JsonWriter out, ResCommon value) throws IOException {
    55             out.beginObject();
    56             writeOBJ(out, (ResSendSms)value);
    57             out.endObject();
    58         }
    59 
    60         protected void writeOBJ(JsonWriter out, ResSendSms value) throws IOException {
    61             super.writeOBJ(out, value);
    62             out.name("eventid").value(value.getEventid());
    63         }
    64 
    65         @Override
    66         public ResSendSms read(JsonReader in) throws IOException {
    67             ResSendSms res = new ResSendSms();
    68             in.beginObject();
    69             while (in.hasNext()) {
    70                 String propertyName = in.nextName();
    71                 if(!readOBJ(res, in, propertyName)) {
    72                     in.skipValue();
    73                 }
    74             }
    75             in.endObject();
    76             return res;
    77         }
    78 
    79         protected boolean readOBJ(ResSendSms res, JsonReader in, String propertyName) throws IOException {
    80             if (super.readOBJ(res, in, propertyName)) {
    81                 return true;
    82             }else 
    83             if (propertyName.equals("eventid")) {
    84                 try {
    85                     res.setEventid(in.nextString());
    86                 } catch(IllegalStateException e) {in.skipValue();}
    87                 return true;
    88             }
    89             return false;
    90         }
    91 
    92     }
    93 
    94 }
    返回类 ResSendSms.java

    具体请求返回代码还是要自己写的,希望API管家继续完善,以后也可以自动生成把URL和请求方法,请求头包括进去,那就更完美了(懒癌又发作了)。。。

  • 相关阅读:
    DataWindow修改的单元格文字颜色改变
    DataWindow奇偶行颜色显示不同
    Retrieve时不清除DataWindow原有数据
    Camera拍照声设定
    使用Log.isLoggable方法
    Android Audio遇到播放无声时的分析
    耳机jack构造及在应用时可能出现的问题
    【Android】使用persist属性来调用脚本文件
    [Android][Audio] audio_policy.conf文件分析
    Android4.4 耳机检测分析
  • 原文地址:https://www.cnblogs.com/bkylacky/p/10766425.html
Copyright © 2011-2022 走看看