科帮网-Java论坛、Java社区、JavaWeb毕业设计

登录/注册
您现在的位置:论坛 盖世程序员(我猜到了开头 却没有猜到结局) 项目源码 > Java实现微信公众平台开发项目源码
总共48085条微博

动态微博

查看: 30185|回复: 1

Java实现微信公众平台开发项目源码

  [复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

    2021-2-2 11:21
  • 签到天数: 36 天

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2015-03-09 20:03:34 |显示全部楼层 |倒序浏览
    java实现微信公众平台开发项目源码
    # ?& J8 A6 J. r% g本文向大家介绍使用Java来实现微信公共平台功能,实现根据回复的内容返回对应的消息。供大家学习使用。
    1 d" Q8 U# L( D微信服务端收发消息接口:WechatServlet.java
    9 B% m/ @& U+ M% p8 C/ i2 [  v
    1. package demo.servlet;
      4 {' c: o1 E. {2 n2 d

    2. 2 ]3 @0 o7 [0 o1 a; i
    3. import java.io.BufferedReader;
      % }* P/ E. q- S* c5 [
    4. import java.io.IOException;
      9 y2 E8 q( u: A* Y' I" P; u5 \8 t
    5. import java.io.InputStream;
      . A. }. n$ F% \! c5 G# O
    6. import java.io.InputStreamReader;
      0 z* ?% R+ O' ^
    7. import java.util.Arrays;
      / y  C* A# V( a" F8 a; L2 W5 u
    8. 5 [, }% ~" ^8 u* z
    9. import javax.servlet.ServletException;
      8 [+ m( K, l. Y! \6 @% z! u) \+ m
    10. import javax.servlet.http.HttpServlet;
      1 D1 c4 M' L4 I: M
    11. import javax.servlet.http.HttpServletRequest;0 a& F3 e: D2 v
    12. import javax.servlet.http.HttpServletResponse;
      + p# g* S: D" D2 Z' H. c/ I

    13. 3 }# p" K( V7 `. h# D* l& m
    14. import demo.process.WechatProcess;; W7 ~* q# w7 @: ^+ ~- C0 J
    15. import demo.util.SHA1;
      " X1 `% k4 g- k5 u( J' Z
    16. /**; M2 R. u; u: |; N, ?
    17. * 微信服务端收发消息接口
      6 F, K2 h6 ]: }
    18. * - ^6 y$ x( V8 k( E  v9 s
    19. * @author 科帮网
      # \7 d/ G" J7 \2 u/ B6 n7 K
    20. *
      " O4 v$ g  {5 f/ ?/ h/ @
    21. */
      % f  U4 I* G: K9 X
    22. @SuppressWarnings("serial")
      8 Z- ]# W! w  N8 k
    23. public class WechatServlet extends HttpServlet {: E8 X) [! v# O) f: ~

    24. + y6 R) v, g+ z7 z
    25.         // 自定义 token6 [& R1 e3 C0 s
    26.     private String TOKEN = "52itstyle";   
      : b- g5 |! N* `; U
    27.         public void doGet(HttpServletRequest request, HttpServletResponse response)
      * R% i0 B8 A) Y; q
    28.                         throws ServletException, IOException {
      : f7 W2 {3 w$ a% Z
    29.                 String signature = request.getParameter("signature");        // 随机字符串
      $ @/ o3 L8 k0 h
    30.         String echostr = request.getParameter("echostr");        // 时间戳6 g9 e4 P; }0 Y9 ]
    31.         String timestamp = request.getParameter("timestamp");        // 随机数" _% [0 j2 d/ b  F2 x
    32.         String nonce = request.getParameter("nonce");$ y7 z  B6 O- v

    33. " A( d% v& r( o
    34.         String[] str = { TOKEN, timestamp, nonce };
      2 w6 x; J1 z' r" W" D, n
    35.         Arrays.sort(str); // 字典序排序8 O, N) v6 H+ M9 y7 j  T1 S
    36.         String bigStr = str[0] + str[1] + str[2];        // SHA1加密
      $ u, n# o2 \4 ^( v+ g; P0 I, i
    37.         String digest = new SHA1().getDigestOfString(bigStr.getBytes()).toLowerCase();        
      9 f' ~: _# S2 g$ w0 X
    38.         // 确认请求来至微信
      1 i: [+ \# e8 K) j) {# ^: ^
    39.         if (digest.equals(signature)) {
      9 C6 {: `# p6 N# x! Z; _, C
    40.                 request.setCharacterEncoding("UTF-8");
      . A+ k# |" S  y
    41.                 response.setCharacterEncoding("UTF-8");8 {3 H" r8 z2 _: L3 F/ Z! _
    42.                 ) M# L  q" J' [: c3 Q
    43.                 /** 读取接收到的xml消息 */
      1 Z, Y) o8 \. U, A7 d- X3 B% e5 ~
    44.                 StringBuffer sb = new StringBuffer();: I+ u  W( g3 G3 O) Z8 W8 @
    45.                 InputStream is = request.getInputStream();
      8 Q5 ~2 u" ^0 U  E+ j: j( _3 N1 d* T
    46.                 InputStreamReader isr = new InputStreamReader(is, "UTF-8");. `. ^8 ^6 T/ K" b- C5 v8 ~; _& U
    47.                 BufferedReader br = new BufferedReader(isr);! f6 Y5 K& x5 a  s$ a  V" j. A) x
    48.                 String s = "";8 d0 B% U! v8 }+ q$ i+ d
    49.                 while ((s = br.readLine()) != null) {3 ]: N% `" }# h  ]! _) g+ g/ A
    50.                         sb.append(s);! W- ~5 [, s) M8 M- y( G
    51.                 }- U# L+ l+ i7 ?& ?3 P
    52.                 String xml = sb.toString();        //次即为接收到微信端发送过来的xml数据
      . D* M1 ^, S$ ]. ?5 N% o
    53.                 ; Q! L: `" ~0 l. }  }0 {  B- n
    54.                 String result = "";" {! G$ ?' B. F; `0 T- V& P2 `. e
    55.                 /** 判断是否是微信接入激活验证,只有首次接入验证时才会收到echostr参数,此时需要把它直接返回 */
      * k% I! o% F# Z# x" H
    56.                 if (echostr != null && echostr.length() > 1) {
      ) i+ l( u$ `5 d! y1 N
    57.                         result = echostr;
      ' |5 @$ D9 A  Z5 g4 F4 R
    58.                 } else {
      - n, }' Q; y' o5 C  _
    59.                         //正常的微信处理流程; V/ c$ x: p, b
    60.                         result = new WechatProcess().processWechatMag(xml);
      7 A1 ^. A7 S% l# ?
    61.                 }
      + I0 H$ [+ y- M. \" B
    62.                 System.out.println("说的什么"+result);
      ; W* s) C! K+ X3 k+ V
    63.                 response.getWriter().print(result);/ t* k4 O) N1 T  O6 q. j
    64.         }  ]4 D$ J& V; U# z! {- m# H
    65.         }! \5 x, N+ D8 l8 Z

    66. - X/ \, B6 z9 q9 m0 b: S# J' F
    67.         /**0 L+ x% F: u( Y* H; n
    68.          * The doPost method of the servlet. <br>
      + {; Z0 V9 P* i! f! _7 Q! T/ s
    69.          * ! ^6 T7 X+ ~; _& q# @3 e
    70.          * This method is called when a form has its tag value method equals to
      7 F$ ~" P( P$ B; i
    71.          * post.
      1 X9 u5 f& Z# N+ a1 E' j" Q
    72.          *
      , ~& h1 @, B( D- o
    73.          * @param request
      # [- |8 D3 a5 g  g. S: x, y
    74.          *            the request send by the client to the server5 \' K# O, Z, r& p0 s% ~
    75.          * @param response
      3 Z- V! f& U7 L7 B$ Y. ?0 h# c- N0 ?
    76.          *            the response send by the server to the client
      ( ]# Y! E! N/ S$ s
    77.          * @throws ServletException/ j: G, B; `, B- L) i+ z) {4 {* g
    78.          *             if an error occurred4 K# Z: V! k/ `3 n6 n# F* X1 O+ `+ R
    79.          * @throws IOException
      ; Q$ }) v+ `- M' ^
    80.          *             if an error occurred7 B  T, h* Z* B7 r
    81.          */
      / p$ e% U; s% C7 J5 z4 H
    82.         public void doPost(HttpServletRequest request, HttpServletResponse response)
      % j0 P. Y/ o6 ?- |4 h4 M7 r% B
    83.                         throws ServletException, IOException {
      $ o; @4 h+ }! N! J0 E. b
    84.                 doGet(request, response);
      " g0 v  b4 R- J# Y' F/ t8 A
    85.         }3 i/ \3 b* i! y- n( {+ s: H9 W: [

    86. ! o$ q. m( _/ s( S& i  [$ F
    87. }
      1 {+ S+ b; u0 E& y4 n
    复制代码
    接收到的微信xml实体类:ReceiveXmlEntity.java6 t/ ~% G" f, I; d( C+ _% N* I& b
    1. package demo.entity;
      + m& O7 w& {! \4 |
    2. /**5 U+ K* }  t# l$ T
    3. * 接收到的微信xml实体类
      , Z/ A- p3 h4 c; I
    4. * @author 科帮网
      6 i1 {" w& t2 _) ~1 x
    5. *1 c+ `5 L. P, ?* y" t% ^
    6. */
      3 ~" k2 m& s% s
    7. public class ReceiveXmlEntity {
      $ h/ A1 C% m9 j" n
    8.         private String ToUserName="";
      & _( a3 c* t/ J4 F
    9.         private String FromUserName="";
      6 }8 F3 Q7 }& c8 ?' o& Q
    10.         private String CreateTime="";8 v- Z7 l  l! c6 A
    11.         private String MsgType="";
      8 \! y3 [" |* c8 b7 c
    12.         private String MsgId="";4 ~: k( x, U0 D, e
    13.         private String Event="";
      8 N# E7 L- d% J+ Z, A
    14.         private String EventKey="";, d+ [$ k2 K/ a$ R0 G
    15.         private String Ticket="";: I" |: |+ ?; e! S
    16.         private String Latitude="";
      & j; I: N: x9 B' ~' I
    17.         private String Longitude="";3 H* [  s0 A8 m! D# N  o. P
    18.         private String Precision="";  c6 }. a) \1 z
    19.         private String PicUrl="";
      ' I0 W! S' ?9 O! c- P7 ~4 g: [
    20.         private String MediaId="";6 [9 P  y! M9 A- v
    21.         private String Title="";& k  N: b& v8 _1 Q8 m7 N3 l4 ^
    22.         private String Description="";' {7 I1 H+ ?" |1 S5 Z  _5 ^
    23.         private String Url="";
      4 c7 E- F0 [2 E/ n, m, t
    24.         private String Location_X="";' V) }" S9 d4 K1 y+ D
    25.         private String Location_Y="";
      3 V' i" X. G3 o! k( V& ^3 x) w
    26.         private String Scale="";
      % o  k& |! f$ C5 W" m
    27.         private String Label="";  D" |, }3 i" t1 t# E
    28.         private String Content="";
      7 j% F# O3 X1 q% @. u: t
    29.         private String Format="";8 g  h" x/ g$ E: E. a3 F4 C) ~
    30.         private String Recognition="";
      $ u* m& Z0 ?/ Q
    31.         8 ?- f. Y% k& c& b$ {6 W
    32.         public String getRecognition() {
      3 l5 h, O1 G) R3 M9 L# h6 [  ]: t& k& e9 F
    33.                 return Recognition;
      ( {, W1 N( K1 V5 M6 B3 u
    34.         }
      ) Z( V3 J' `- t$ N0 t0 `
    35.         public void setRecognition(String recognition) {
      7 U5 J8 D/ U' T
    36.                 Recognition = recognition;" t4 V0 E- H: K+ Q
    37.         }7 E4 ~8 `2 w+ I0 {
    38.         public String getFormat() {
        p! z" O6 Y9 q% x* F% ?
    39.                 return Format;, [, k* Z; W' N' m
    40.         }3 a! N  ^/ H/ d, D! c
    41.         public void setFormat(String format) {
      6 W8 T3 v/ p! ?$ b) V" ~5 `4 X
    42.                 Format = format;2 A! {. }* w" @$ h# F3 n: c4 \
    43.         }. {- R. W8 j+ S$ S1 ?! o: G3 B
    44.         public String getContent() {" B. Z. r7 A" p, ?- w
    45.                 return Content;: M! y  M- t4 `( B. {! P6 L
    46.         }3 D& l- \0 f4 i  A7 W1 G
    47.         public void setContent(String content) {
      / V; v% `6 h0 ?$ V  r0 S8 R: [" K
    48.                 Content = content;( C$ J  Q" Y4 L# f3 u/ I4 ]7 m* E. F
    49.         }9 J' {, u" {( H" z6 z
    50.         public String getLocation_X() {
      ; b4 F( }, r5 Y" s
    51.                 return Location_X;% S: ~5 h: a/ p/ n
    52.         }
      ) _. B# E( n# W! ^0 G) W. C8 E) c0 I
    53.         public void setLocation_X(String locationX) {% ~# I4 \+ v* V
    54.                 Location_X = locationX;& e( ?- }3 s- Y2 g) d( N2 m4 q
    55.         }% H) u4 F( s# j$ B
    56.         public String getLocation_Y() {" ]/ y* M  G  @+ q3 g# V7 W! J
    57.                 return Location_Y;8 \$ W, p  P" |* }. i# z9 l0 F! q* s
    58.         }
      3 V6 l1 ?' y! a6 w% T2 Q8 g
    59.         public void setLocation_Y(String locationY) {
      ! I# u1 T$ F' L
    60.                 Location_Y = locationY;
      1 F, u) w6 e) p" G- `- ^  ^
    61.         }) f2 k! b$ l* X- f" N
    62.         public String getScale() {. \5 a. C7 D, _; N! f4 u8 x% _) Z
    63.                 return Scale;! G2 Y/ `+ B5 L. B; a3 S* O$ f: M
    64.         }" |6 _% B3 c: S8 R1 E
    65.         public void setScale(String scale) {
      # N7 a% O6 ^7 g* @
    66.                 Scale = scale;
      3 M( G/ ^* S3 e# L' w, f" H
    67.         }+ K* B8 [' \2 y8 Q9 I" I* |* J
    68.         public String getLabel() {
      $ \7 Z# ^" x4 W4 ]/ t3 _( f% A5 O8 N
    69.                 return Label;
      2 X, N; g9 |3 r' j
    70.         }9 E5 {3 l" c( l# v% @! v" b% x3 P& s
    71.         public void setLabel(String label) {
      0 r0 ^$ q) |9 S4 ^; p
    72.                 Label = label;' k! s  e. a; d( s) h$ U( {
    73.         }' q- l  a9 e# W* w' E
    74.         public String getTitle() {
      , s/ D$ l  O; P* m, q
    75.                 return Title;
      ; K& S& r2 Z4 j, ]
    76.         }# c8 ?( b- M& g* G3 P4 L
    77.         public void setTitle(String title) {
      / ?7 E6 j3 p" B8 x* v7 O
    78.                 Title = title;
      7 G( ~* J: |' @; p+ f6 m) i
    79.         }& p. X# o" M& c$ F- ~& w
    80.         public String getDescription() {
      : x4 x; w' R% {6 X  P4 ^# \
    81.                 return Description;
      + Y- b9 O9 W7 `; }! m# q+ [0 B
    82.         }  n# ?* d( ^8 q
    83.         public void setDescription(String description) {$ g' x, n9 Q8 P$ m0 X
    84.                 Description = description;) s6 P$ J7 |% a! l9 t. H0 |
    85.         }6 G5 U" H. @& ^3 z2 L, i
    86.         public String getUrl() {
      7 o1 [+ S' L  J: j* W* G
    87.                 return Url;
      ( _, V9 M4 B9 r/ Y1 |1 ?$ O
    88.         }
      + s* N3 b" E1 x7 A% C+ G
    89.         public void setUrl(String url) {( w$ t! _- w! K* J- \2 f9 Y
    90.                 Url = url;
      ' B9 Z) R9 `( f1 s+ K7 E
    91.         }& w" ?- x2 R+ V
    92.         public String getPicUrl() {/ z' g0 A0 E9 P6 F7 c. X7 c
    93.                 return PicUrl;6 u+ |: H4 S$ j2 \* l* q* a
    94.         }
        M: l: N- {( |( Q" t% B
    95.         public void setPicUrl(String picUrl) {
      % a6 Q( s% r  s1 z2 A$ Y
    96.                 PicUrl = picUrl;
      / z6 i0 Z3 V8 k0 V1 m$ e
    97.         }
      - ~) O/ c; f0 M: v2 _  q4 g
    98.         public String getMediaId() {
      ' _. S$ S' w4 G! C8 w1 m8 Q- ~
    99.                 return MediaId;
      ) @4 }1 k" ]+ |2 j$ c* g( h
    100.         }9 q$ T9 r$ n4 ~3 f0 O6 l
    101.         public void setMediaId(String mediaId) {3 V2 W4 ^: G- W7 P
    102.                 MediaId = mediaId;
      4 }: U, I! }: _8 y
    103.         }
      ) x* M. F% v& e0 y% Q
    104.         public String getEventKey() {
      0 F  o0 C! A2 L4 f. d/ p! L
    105.                 return EventKey;
      * y' G& H( e9 u- M0 O+ [! O
    106.         }
      . K. L8 Q; b; x5 l6 q! S0 V
    107.         public void setEventKey(String eventKey) {
      % B6 |2 K9 e! T; V- r5 q
    108.                 EventKey = eventKey;
      + b+ ]4 w% L- _. F* I' j* g
    109.         }
      - f& Q5 U# ^) b$ I: U# |" _" m0 V
    110.         public String getTicket() {
      ' O  h$ v6 h6 m1 V7 j" E
    111.                 return Ticket;3 z5 H4 n$ Q. c; d9 P& U
    112.         }# b2 X! v+ Q  a  I# d6 \
    113.         public void setTicket(String ticket) {' ?$ J' ~* p& n" y
    114.                 Ticket = ticket;; w8 t7 K* H4 i- f, C
    115.         }
      9 d' P3 A* c/ D, R) c2 X
    116.         public String getLatitude() {, F7 `& V7 J, ]7 V% h2 _
    117.                 return Latitude;2 j2 A0 V+ w2 W7 F
    118.         }; G# k+ ^- J$ F; g. ~% a# h# q
    119.         public void setLatitude(String latitude) {
      5 s" J( ^, N  e2 Y6 F& j
    120.                 Latitude = latitude;, o- W' `2 E7 y2 {
    121.         }
      ; ?6 s: t" ]1 \2 e
    122.         public String getLongitude() {$ o6 |4 t8 I  @. c
    123.                 return Longitude;* m) W! j' L2 ?3 A
    124.         }. ^2 ]# U! W4 h
    125.         public void setLongitude(String longitude) {
      % H0 N  D. s0 u; P
    126.                 Longitude = longitude;
      * V3 z6 \8 e4 f  G
    127.         }
        l, i9 D% R4 y) Y3 E) |7 H& O
    128.         public String getPrecision() {6 ^: x( x- l! W
    129.                 return Precision;
      # W' F/ k: [9 [, i7 o9 A% b7 s2 T
    130.         }  Z6 H; [7 G3 q
    131.         public void setPrecision(String precision) {% |, a7 e! K7 M9 \9 O- w: @
    132.                 Precision = precision;6 A* l! T$ Z& t$ A- o
    133.         }7 H/ X, P) B& w6 ^) M. E( ^' q4 C
    134.         public String getEvent() {
      & }. B, {& x1 z- \5 ^0 ]6 ]* o
    135.                 return Event;
      3 G* V' H5 U( E* J% Y3 i
    136.         }9 q8 T: t; N! L- _
    137.         public void setEvent(String event) {& J( H# A" v: p  A3 }+ G* \+ P8 P
    138.                 Event = event;
      5 n' X5 R, ]( S, G1 q: h
    139.         }
      3 @: p: H7 v: i6 g+ U9 G! O
    140.         public String getMsgId() {% f# |# W# @, F/ r4 X+ O' |
    141.                 return MsgId;- m) v9 g5 n7 R( ~# v2 a; z( {
    142.         }
      / N6 U2 q) w4 ^3 A) k7 t
    143.         public void setMsgId(String msgId) {2 k  F1 ~  Q6 F7 V* D. X7 r
    144.                 MsgId = msgId;9 _' n3 c2 T5 s" E# s
    145.         }
      & ^6 M- E' z# x3 V" n
    146.         public String getToUserName() {
      ) Q1 S  \8 R+ H/ `& y
    147.                 return ToUserName;
      * d0 b' s0 E. c3 t7 Q
    148.         }
      : ]* y, H# L, j0 W( [0 T0 u
    149.         public void setToUserName(String toUserName) {
      9 k" Y" r1 i1 Q1 C
    150.                 ToUserName = toUserName;
      4 e* ?' i1 U5 @& b  O: `
    151.         }! ]: c, N" |( z* c% @
    152.         public String getFromUserName() {7 g+ C! O) U+ E8 v
    153.                 return FromUserName;# r" C; o! f7 z+ }
    154.         }  ?) l+ B: ]+ G  E" m- q0 T
    155.         public void setFromUserName(String fromUserName) {& Q, F7 S. }* @2 S) ?
    156.                 FromUserName = fromUserName;
      4 I3 Y3 [2 i# Z
    157.         }/ _, L( S* K2 ^; |2 T
    158.         public String getCreateTime() {5 D4 h6 y% a) ^; P
    159.                 return CreateTime;
      # Q, B9 m" s" Z- O% W; c+ S
    160.         }
      0 h: u! ]3 G4 g8 ?* Z
    161.         public void setCreateTime(String createTime) {5 `* O! a( I6 E. ?' Q9 S2 B4 F/ d8 [
    162.                 CreateTime = createTime;
      6 x% @6 \( f1 Z5 _! j1 C6 ?
    163.         }  t1 M( K& R$ R; z# E
    164.         public String getMsgType() {& h5 X" A/ H2 i( z7 C4 v1 Y
    165.                 return MsgType;
      5 a) f: A. R5 C
    166.         }
      3 l# d, q9 j3 S( m- L
    167.         public void setMsgType(String msgType) {
      6 N$ w+ `" J; }0 @3 V7 y
    168.                 MsgType = msgType;' E$ n( A% _$ S. f8 I3 g
    169.         }
      4 c8 f, Z$ A% L* r0 h
    170. }
      / H# b# a! f. m! u2 _( L
    复制代码
    调用图灵机器人api接口,获取智能回复内容 TulingApiProcess.java2 A4 A& \' \5 C

    8 Q: I3 X0 z# ^' d7 h8 Z0 v" F
    1. package demo.process;7 Y# I* T- t) J0 K8 l) s
    2.   Z* S( d- {# \1 O$ d: U
    3. import java.io.IOException;3 l* H8 B! b2 n0 f- ~$ L
    4. import java.io.UnsupportedEncodingException;
      6 B: c. {" J# d4 E
    5. import java.net.URLEncoder;
      + l) R% v* N6 K  I. Y

    6. ' d/ K  Y' w1 m- P/ H( N
    7. import org.apache.http.HttpResponse;5 B4 _+ l% h( X5 M8 s
    8. import org.apache.http.client.ClientProtocolException;
      - @' U: Z$ T6 i) i0 Z% I; ]
    9. import org.apache.http.client.methods.HttpGet;5 h; }2 d  x0 D% K5 N8 l
    10. import org.apache.http.impl.client.HttpClients;
      ( {, p# B2 u  |/ x; h% G' A0 ~
    11. import org.apache.http.util.EntityUtils;; i" ]1 f/ t* f6 H' P0 }8 p0 p
    12. import org.json.JSONException;
      ) ^/ V( F2 i0 n$ j- K: |; f
    13. import org.json.JSONObject;
      . G0 v8 H) [- q9 }5 ]' R# z

    14. 8 Z# ~9 e/ L& n$ H* Y+ ]6 l" L
    15. /**
      ' w2 o. E0 _$ p: `5 k
    16. * 调用图灵机器人api接口,获取智能回复内容2 s8 v8 N$ P- k( Y9 e
    17. * @author 科帮网
      + L1 B( d' s7 f
    18. ** ^0 b: D" N8 y, ]4 L
    19. */$ l& C& z4 w6 O
    20. public class TulingApiProcess {
      . Z' D% c6 E# f3 t# R, |5 L
    21.         /**
      ! I$ U. G. P0 i- u4 `0 E5 a
    22.          * 调用图灵机器人api接口,获取智能回复内容,解析获取自己所需结果; ?2 I% f% P% d5 o, ?9 @- h, j
    23.          * @param content
      6 ?7 S7 W  \5 t8 r* o) h
    24.          * @return
      - @9 H  N/ V* i
    25.          */$ N3 X/ \6 I) s7 b% w
    26.         public String getTulingResult(String content){
      : c/ C0 B3 x+ X! W( S9 B9 x% ^# R; ?
    27.                 /** 此处为图灵api接口,参数key需要自己去注册申请 */
      ) |5 Q! S2 p: `# K
    28.                 String apiUrl = "http://www.tuling123.com/openapi/api?key=2a31b2f601f74b54ea13db1c82fe5d71&info=";
      & N6 N0 V4 R* Y; g% @! O3 e; E) f
    29.                 String param = "";
      , z! z" {# _5 h( {, a. ~
    30.                 try {
      ( w; r& X9 N7 D. P& D2 d* d5 o
    31.                         param = apiUrl+URLEncoder.encode(content,"utf-8");
      ) N+ o+ C+ y7 {4 D- x$ U
    32.                 } catch (UnsupportedEncodingException e1) {
        O% l% v9 _! o4 ~* I7 F5 n3 l
    33.                         e1.printStackTrace();7 H! H& T9 w: @* T6 D
    34.                 } //将参数转为url编码
      1 K: i# x0 i" P# Z3 v
    35.                 /** 发送httpget请求 */
      % e- V3 R0 d* H; G
    36.                 HttpGet request = new HttpGet(param);
      ) w( t9 o1 M) f3 O9 \
    37.                 String result = "";" m9 \" _! n. `+ B
    38.                 try {1 ?* l% C- d* v. Q4 ~2 h
    39.                         HttpResponse response = HttpClients.createDefault().execute(request);6 l% K' C1 E: r  A( r
    40.                         if(response.getStatusLine().getStatusCode()==200){
      1 R5 B* ]- P' s  g  @% Q. j8 K" k
    41.                                 result = EntityUtils.toString(response.getEntity());; E3 b! g4 Q( a/ U4 g& b% l3 \
    42.                         }$ m0 R6 \4 J; p7 r
    43.                 } catch (ClientProtocolException e) {
      ; n; |- R8 {5 i2 f( Y2 t
    44.                         e.printStackTrace();2 O# i, @) |. p( j8 \1 E9 b# `; Q
    45.                 } catch (IOException e) {7 B5 z0 @+ {. l' R# l' [- V6 p) D
    46.                         e.printStackTrace();0 G0 O! \$ U0 ^) N7 k- g) \4 }
    47.                 }
      % r" ^- j+ b% u. F
    48.                 /** 请求失败处理 */
      8 R6 f& F0 C$ K
    49.                 if(null==result){: M5 P" F; H2 N0 B3 R$ @
    50.                         return "对不起,你说的话真是太高深了……";2 ~# X+ ~+ v( u3 C; g+ x
    51.                 }
      ! u' d  r# L% m0 ]
    52.                 ! s+ W0 w% t% b
    53.                 try {! e9 _3 d( r; Q2 M
    54.                         JSONObject json = new JSONObject(result);# t0 }, F) R, P4 G& C9 r
    55.                         //以code=100000为例,参考图灵机器人api文档) P* B' s% z/ U# b: G! a+ m
    56.                         if(100000==json.getInt("code")){
      2 W2 J* y0 Q$ m( k/ w
    57.                                 result = json.getString("text");
      9 F3 Q% M" \; M* U) r7 [$ {, ~
    58.                         }
      3 P, ^5 u; O3 I0 V  Z+ z
    59.                 } catch (JSONException e) {
      9 _" p0 e+ K4 [: ]( ^4 U" R
    60.                         e.printStackTrace();' `) U1 W( H' j8 `6 G; \8 P; |" V
    61.                 }; V7 Y+ x0 q! P
    62.                 return result;
      9 g( d1 W7 I7 k! ^
    63.         }
      8 }% V" a" `$ V, q9 d2 v
    64. }
      ( K% x+ E; g) j# l- v- a
    复制代码
    3 q, _0 b  o4 F6 X8 a) w
    web.xml 配置:" `8 {. ]9 ^! S
    1. <?xml version="1.0" encoding="UTF-8"?>' Y) T- J  |, Q$ o, j( b
    2. <web-app version="2.5"
      , h2 _- i/ T6 P& F9 H
    3.         xmlns="http://java.sun.com/xml/ns/javaee"
      ' ^9 J3 d. a4 y7 o& V
    4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      3 R4 g* _1 j2 a" ?9 t
    5.         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      ; H, L5 L. O3 q4 K  V
    6.         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  k% O3 u' H# B! N9 i
    7.   <servlet>
      . h. T6 z% R1 q. i4 g( L
    8.     <servlet-name>WechatServlet</servlet-name>: w' u, \& m$ U  L/ [: M# [
    9.     <servlet-class>demo.servlet.WechatServlet</servlet-class>% T" f0 m4 q9 k( Y3 A
    10.   </servlet>5 D; p; z4 Q( V2 h* L

    11. . \, V( o5 C  h* {
    12.   <servlet-mapping>' l" O2 k3 Q6 P4 m% M
    13.     <servlet-name>WechatServlet</servlet-name>5 S7 M: h) A, q" V
    14.     <url-pattern>/wechat.do</url-pattern>
      0 H' K3 f. Q( S+ h
    15.   </servlet-mapping>
      2 q+ o# u' H* }/ F* v. T4 z+ F
    16.   <welcome-file-list>! {( h1 `# d7 p' m0 U- Y6 r
    17.     <welcome-file>index.jsp</welcome-file>9 s0 G: A% u! O6 i8 u
    18.   </welcome-file-list>6 z- Q0 o0 r0 I, f9 F
    19. </web-app>
      % S# S0 h) J* I8 X: F+ F5 ]
    复制代码

    1 m- Q: ]  ~1 y, g  h8 C项目源码下载地址:4 C. n. S: f+ k, Q" S
    游客,如果您要查看本帖隐藏内容请回复

    + \# A2 m3 b. J) h, w
    9 r4 P+ m  A( F6 d9 c2 Y, t

    科帮网-Java论坛、Java社区、JavaWeb毕业设计 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与科帮网-Java论坛、Java社区、JavaWeb毕业设计享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网-Java论坛、Java社区、JavaWeb毕业设计的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、科帮网-Java论坛、Java社区、JavaWeb毕业设计管理员和版主有权不事先通知发贴者而删除本文


    JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

    红红火火恍恍惚惚
    admin    

    1244

    主题

    544

    听众

    1万

    金钱

    管理员

  • TA的每日心情

    2021-2-2 11:21
  • 签到天数: 36 天

    [LV.5]常住居民I

    管理员

    沙发
    发表于 2016-06-12 14:41:55 |显示全部楼层
    逸轩居士 发表于 2016-6-12 06:18
    - G9 C6 _, N1 j4 Y* d8 t; y" y) X( Z功能强大值得入手
    6 @0 u9 J* H8 `" `2 |# Z8 C
    哈哈哈哈哈
    回复

    使用道具 举报

    快速回复
    您需要登录后才可以回帖 登录 | 立即注册

       

    发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
    快速回复 返回顶部 返回列表