科帮网

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

动态微博

查看: 34567|回复: 180

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

  [复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    #
    发表于 2015-03-09 20:03:34 |只看该作者 |正序浏览
    java实现微信公众平台开发项目源码# t  C! \0 X2 {
    本文向大家介绍使用Java来实现微信公共平台功能,实现根据回复的内容返回对应的消息。供大家学习使用。
    5 E% X" |* [, D( x4 K微信服务端收发消息接口:WechatServlet.java
    5 a% ?' w, w0 S" {0 Z
    1. package demo.servlet;2 @3 f. B) t% i$ A* i
    2. ' w& V" q9 G8 u9 l- X1 q1 h) ?
    3. import java.io.BufferedReader;) K* v! @+ P" j" a- m/ c7 x# @
    4. import java.io.IOException;0 X" f- A! p* [$ @
    5. import java.io.InputStream;- p; P  M2 {( h
    6. import java.io.InputStreamReader;
      8 p( e2 ~+ [2 |
    7. import java.util.Arrays;6 I: K* S9 I3 X- F  j
    8. 3 N  y4 j/ t3 m( T, v/ d7 n3 J
    9. import javax.servlet.ServletException;( Y  H2 W9 i% h0 h
    10. import javax.servlet.http.HttpServlet;: ]' d( z; [' f/ }* ?
    11. import javax.servlet.http.HttpServletRequest;
      # Z& n3 C' s$ t# y% s+ d
    12. import javax.servlet.http.HttpServletResponse;+ y3 H; r( R% m/ k, J/ L
    13. 3 v0 Q% V. [8 A0 G9 s
    14. import demo.process.WechatProcess;+ w* q, Z* L* W# S
    15. import demo.util.SHA1;
      6 @. Y5 U' C' a
    16. /**# j6 y! e% h7 a! Q
    17. * 微信服务端收发消息接口
      7 Q/ q( R+ t; R: S$ E& \; y
    18. * ( [" F8 l  C4 B- I* I4 z
    19. * @author 科帮网
      1 C8 m; |2 T) ^; K; C' M
    20. *
      6 r+ x+ i3 @+ y; J& e
    21. */
      / V- l0 y; {- D) j* w
    22. @SuppressWarnings("serial")$ f; s, i# i) x/ X$ W
    23. public class WechatServlet extends HttpServlet {
      % {6 e1 d2 H2 \4 z% e

    24. % t) y% O5 _7 D/ S7 r8 E. P: [  i
    25.         // 自定义 token
      3 I5 f) S7 V5 v% }
    26.     private String TOKEN = "52itstyle";   
        |  D0 [2 C% Z* }4 m3 ]* Z; x$ @
    27.         public void doGet(HttpServletRequest request, HttpServletResponse response)% O8 }; z0 }0 m. \
    28.                         throws ServletException, IOException {
      # s8 J' E: I) `9 g$ h1 C
    29.                 String signature = request.getParameter("signature");        // 随机字符串( o' e5 z8 b! P0 l0 I7 V' C) R
    30.         String echostr = request.getParameter("echostr");        // 时间戳
      5 p. R4 o3 J6 d( {6 ?6 s
    31.         String timestamp = request.getParameter("timestamp");        // 随机数
      9 h# W: }  ?5 ^" c5 w! \
    32.         String nonce = request.getParameter("nonce");/ R6 Q5 N. t) z1 }" u( M9 j0 |

    33. * {" @* @* @& S! v# A; a, J
    34.         String[] str = { TOKEN, timestamp, nonce };/ k  `0 v5 p$ b& z
    35.         Arrays.sort(str); // 字典序排序. A0 F' y5 M: i
    36.         String bigStr = str[0] + str[1] + str[2];        // SHA1加密$ X+ p& X1 C, J" r
    37.         String digest = new SHA1().getDigestOfString(bigStr.getBytes()).toLowerCase();        
      6 s" c8 ]# F  J2 s7 ?) R7 _+ R
    38.         // 确认请求来至微信
      . E( r) v( {# I  P2 |/ F* }/ L
    39.         if (digest.equals(signature)) {
      3 t9 G7 w8 o; e3 J
    40.                 request.setCharacterEncoding("UTF-8");
      ) h" B2 j: R, d% M
    41.                 response.setCharacterEncoding("UTF-8");
      ) C( O5 ~* I% A; f% Y
    42.                
      ( S& F) ~2 A( N8 B
    43.                 /** 读取接收到的xml消息 */' Q  x2 R! a1 e9 F: I( E
    44.                 StringBuffer sb = new StringBuffer();3 A% h1 T' a! q$ i; A
    45.                 InputStream is = request.getInputStream();
      ( Y0 S2 l! W/ `! j
    46.                 InputStreamReader isr = new InputStreamReader(is, "UTF-8");
      % d, d. I; t5 x3 c; g- m7 i, j
    47.                 BufferedReader br = new BufferedReader(isr);
      & [5 n5 [# l. q8 y) g2 {
    48.                 String s = "";9 T- \- A$ E/ d" v+ z4 v' x6 P
    49.                 while ((s = br.readLine()) != null) {7 [6 v9 P4 R/ d8 [+ {
    50.                         sb.append(s);% a$ a& k8 X4 c3 X/ R9 [, \
    51.                 }
      3 E" }5 s6 {8 l3 h( y9 ^  i# V
    52.                 String xml = sb.toString();        //次即为接收到微信端发送过来的xml数据& V5 B4 I1 _$ V
    53.                
      $ N9 a. A, F. g- Q' L2 X
    54.                 String result = "";
      + H# m2 [8 @+ u2 j
    55.                 /** 判断是否是微信接入激活验证,只有首次接入验证时才会收到echostr参数,此时需要把它直接返回 */' y# l8 _: q) ]
    56.                 if (echostr != null && echostr.length() > 1) {7 b! h/ t, f& a5 F% h
    57.                         result = echostr;
      ( o7 q$ Z, O0 h1 R1 B
    58.                 } else {% v; r: m) X; [
    59.                         //正常的微信处理流程4 W; P2 X( ?! V$ z/ w4 D
    60.                         result = new WechatProcess().processWechatMag(xml);
      2 |- l  [3 v2 f6 f. r% z
    61.                 }, N, \; z# X/ X
    62.                 System.out.println("说的什么"+result);2 C% m& ?7 @' [0 c
    63.                 response.getWriter().print(result);- J4 A6 l. p$ C( p0 k
    64.         }
      5 L5 b3 S) Y$ e% x
    65.         }
      ) e$ o! o0 ?4 y* K7 M
    66. 2 ?2 F0 \6 N% H5 C1 n
    67.         /**$ r  B$ Z1 k, H  B
    68.          * The doPost method of the servlet. <br>7 |" @6 e( U2 [2 g3 G
    69.          *
      , X$ v2 y( M7 a- m
    70.          * This method is called when a form has its tag value method equals to
      5 D, }4 |& B1 }# e+ }$ Y) {4 ]
    71.          * post.2 j- }' a2 G) S# O7 \
    72.          * ( Q7 n) t2 i4 Z3 M
    73.          * @param request
      ' v6 j4 w2 \$ ?4 t
    74.          *            the request send by the client to the server  n9 ^/ V9 t2 K$ l/ b' s) A8 }) Q% K/ u
    75.          * @param response1 f7 E% j( U/ U( b! u' |2 |
    76.          *            the response send by the server to the client8 ^# V, C6 H0 R* E
    77.          * @throws ServletException
      % b/ @5 X% p0 ]' A$ B) \
    78.          *             if an error occurred
      : D- ^9 {- m4 d4 c$ B7 _" r5 O% K* A: Y
    79.          * @throws IOException
      3 v  `# X- D5 k% u) @
    80.          *             if an error occurred
      9 W. o+ Z# P% C& f/ f/ X
    81.          */
      ; g: ?% h& r; r
    82.         public void doPost(HttpServletRequest request, HttpServletResponse response); X& I  Y. E# ~8 ]. |
    83.                         throws ServletException, IOException {
      4 w0 [* W# l/ @& |! l+ L
    84.                 doGet(request, response);
      $ M- @% G, l% f1 ~
    85.         }* v% D% H, n. \

    86. ' B; c) [+ n9 o8 |" L
    87. }
      / W1 Z7 a: e1 w! S
    复制代码
    接收到的微信xml实体类:ReceiveXmlEntity.java5 P8 a' ~6 g) i: f5 ^' j. o1 S$ i9 _
    1. package demo.entity;; H# \% G. @2 |3 I" \
    2. /**
      $ p7 }" V7 v+ a- }$ g* k, f
    3. * 接收到的微信xml实体类
      1 Q- U, e5 ?3 t' A( w% @
    4. * @author 科帮网
      8 @7 i( {7 g% \* H6 K/ E8 D
    5. *; ~6 A5 F* u; m% s3 S. q
    6. */- l; E: p! H$ K* ?
    7. public class ReceiveXmlEntity {/ J" }* B- o! a) g) r1 n, j1 n
    8.         private String ToUserName="";* _# j8 P$ p; F& X5 H/ v9 C
    9.         private String FromUserName="";
      * y# j4 N( m  U3 g1 F
    10.         private String CreateTime="";
      0 Y: V1 M1 d1 i" N1 u* a2 [
    11.         private String MsgType="";
      , y0 K: t4 P' n1 b$ G4 l
    12.         private String MsgId="";
      + y; ]0 M/ F0 a' ^+ |
    13.         private String Event="";
      ; j7 w1 {4 W) A3 c& F( I
    14.         private String EventKey="";
      ( j1 [4 c1 M/ t% F9 G; n
    15.         private String Ticket="";- X  t' `# B  w5 f& q5 T) N0 ~
    16.         private String Latitude="";
        P3 H9 w3 {$ p! f# i) f
    17.         private String Longitude="";8 Y+ O) ^" W( [6 s! T" m
    18.         private String Precision="";& S0 |8 V8 f: Z/ M
    19.         private String PicUrl="";
      4 J: g! S( |& w/ |1 L
    20.         private String MediaId="";7 U$ K( ^7 I" l6 w5 A
    21.         private String Title="";: a$ `: k2 }4 D( D6 u3 z* ^4 M
    22.         private String Description="";
      3 C* U1 n3 H" _. L+ {2 h/ S* e
    23.         private String Url="";
      3 b: p3 T. `8 Q8 O& y
    24.         private String Location_X="";2 `/ P6 R) ~9 L$ ~. W) I- R+ s
    25.         private String Location_Y="";) _5 h( I7 y& l; |. ?+ ^) f
    26.         private String Scale="";* i9 F9 v( L% F% n5 X6 t- R
    27.         private String Label="";! p2 _* S! a3 a- z! e6 n3 f
    28.         private String Content="";
      # [% ^6 l# q) G/ d8 [4 h1 N
    29.         private String Format="";) Z1 G/ M  p" r
    30.         private String Recognition="";3 |2 V% e) L7 h
    31.         
      + M7 u. W( h. M: b" P5 Y( a
    32.         public String getRecognition() {# j7 H' S" K) O8 D0 j
    33.                 return Recognition;* t4 p  T0 E1 }/ P
    34.         }$ \! @+ g1 m9 |
    35.         public void setRecognition(String recognition) {
      1 n" G, ~5 N8 u. S& F6 I
    36.                 Recognition = recognition;
        D* F1 F8 w, P3 Q" V- x; K' b
    37.         }3 I% s  O2 h" V! Z3 Z( J
    38.         public String getFormat() {
      6 M9 f  G* J" n) i
    39.                 return Format;* P6 D, }: A8 h: L) K6 T* E
    40.         }; D* r) _; j0 d# Q; N0 J
    41.         public void setFormat(String format) {
        L$ }: A  l, G% \0 }3 V
    42.                 Format = format;+ @2 u" q9 ?; w5 d
    43.         }
      * d$ ^, j, u) i+ H- |
    44.         public String getContent() {6 i4 F+ h( m8 j7 p# f3 W
    45.                 return Content;
      - {3 M, `9 k9 ]3 p+ o/ w$ s
    46.         }
      / I- w  k( r  V9 H  u
    47.         public void setContent(String content) {, K/ b) G, A; K. Y; v) d
    48.                 Content = content;
      8 U6 z" W8 r4 c. B( |" ^3 `
    49.         }
      ; r; d. Z* R+ j  f8 y2 D
    50.         public String getLocation_X() {% p9 x* X) N. V- B, [- D; i. _
    51.                 return Location_X;3 B! A6 {. ~. y* y4 k) \% w1 x2 ]
    52.         }
      ; }! u# c' f! i* Y* i
    53.         public void setLocation_X(String locationX) {. c' P& a4 s+ z
    54.                 Location_X = locationX;$ a8 U8 i  Z) P" Q1 s$ c
    55.         }! n0 Y  }& w. J9 T( g# q& r" g
    56.         public String getLocation_Y() {
      - i4 U7 S5 o) g7 |- m$ n/ D
    57.                 return Location_Y;
      & P1 q3 T! Q- y) S- _  g
    58.         }. s% C) r/ \) I- X; T
    59.         public void setLocation_Y(String locationY) {
      1 `! ~& _, D  Q4 s" I8 H  l) ~
    60.                 Location_Y = locationY;
      ) l0 K% U) k4 r' a0 l
    61.         }
      2 k0 y* r8 @% M9 `& x9 {! U
    62.         public String getScale() {+ X3 N# r0 H0 J0 s% y. i! o, x
    63.                 return Scale;
      # c  J) `- H2 z* m# Z0 r
    64.         }
      . _5 Q+ A& h( T1 }( n# N; A
    65.         public void setScale(String scale) {' X8 T' @" [1 f$ G- @" H
    66.                 Scale = scale;" R1 f* W& n# k4 f5 b
    67.         }
      8 Y9 |  S( @4 c$ m
    68.         public String getLabel() {5 k  s' `/ _, h, C* k" z9 Z
    69.                 return Label;
      7 u! @( j1 `! p: c; b% s, R+ ^
    70.         }
      " ?# X4 ]2 R9 z+ r* l
    71.         public void setLabel(String label) {
      & o6 [( r7 k2 x. X# B6 R, W# e
    72.                 Label = label;
      - C# @( [! }: A3 S( g+ C
    73.         }
      * z9 e, }/ r" U4 O) y; }, P
    74.         public String getTitle() {
      0 a6 b6 V7 S. J9 H
    75.                 return Title;
      ) {9 b) m  S' i& M
    76.         }/ [8 K% Y& s/ |( W1 h
    77.         public void setTitle(String title) {5 S1 g# P: k, K0 p
    78.                 Title = title;
      * W, t0 O& o4 {; O1 Y& s4 E9 e+ u
    79.         }
      . w; g/ Z+ a7 F/ |( x$ u
    80.         public String getDescription() {$ }0 b3 o8 m3 V. B7 z0 V
    81.                 return Description;
      ) Z) y+ k+ F4 G
    82.         }; O) a) k8 Z7 A/ l" u
    83.         public void setDescription(String description) {
      ; d' F5 C. m3 @8 y0 ]
    84.                 Description = description;% e% u  L9 t0 f- V
    85.         }
      % @: Q$ @- m; x! L9 W" m( f/ K, i
    86.         public String getUrl() {: E5 a# k4 H) d7 A& ?
    87.                 return Url;
      ! t& s4 g8 R0 B4 I' R
    88.         }% F2 w5 x; a, [0 i' O; l! r
    89.         public void setUrl(String url) {
      ; Q# ^% f. U0 N- X# ^4 c# k. p6 f: O* U
    90.                 Url = url;
      / a* j/ u; A/ r& h' E  B' n
    91.         }, `7 E$ u; F& o( D0 g6 o3 b
    92.         public String getPicUrl() {
      0 V' y7 M  r3 |( M( X
    93.                 return PicUrl;
      3 E# j6 c. c' O2 F
    94.         }
        Z( c# N6 R/ \" Y8 Q
    95.         public void setPicUrl(String picUrl) {2 e" A, P* K4 M
    96.                 PicUrl = picUrl;
      4 K% r- }2 t# Z
    97.         }
      ' e) G: E# B, |6 O8 z: S( m0 Q
    98.         public String getMediaId() {
      4 o) G) a9 g" K; F
    99.                 return MediaId;
      % K% d8 v# E% a( o' X
    100.         }3 V8 e' Y7 R5 k
    101.         public void setMediaId(String mediaId) {
      6 C$ ]+ x9 P" C9 C, Q4 j1 j7 U
    102.                 MediaId = mediaId;/ w1 @+ j( P. f
    103.         }
      ; |' w& _  w+ H$ B: i6 V( x
    104.         public String getEventKey() {
      1 V1 [+ z* a; w2 V# w# k" i
    105.                 return EventKey;
      ; R# Q# H9 R6 E6 D) h* x& q  C
    106.         }) L  Z( T9 I' @) @+ U0 r4 S
    107.         public void setEventKey(String eventKey) {
      ! S) e' b" E/ x" j4 l. ^" c
    108.                 EventKey = eventKey;
      3 ?; r: {5 N" G4 I* H9 f
    109.         }% E! H9 l7 T# H' N5 r
    110.         public String getTicket() {8 u, `6 ?3 A' t$ g# Z  D
    111.                 return Ticket;
      - C* z- ~* ]; O! F# }# Y
    112.         }
      4 {/ V! j1 [- O/ U
    113.         public void setTicket(String ticket) {$ x* {4 ], S+ K
    114.                 Ticket = ticket;
      3 [, s, N/ N8 s4 H
    115.         }
      - t5 u6 U1 h1 ?, Q
    116.         public String getLatitude() {* k, v$ y# `  j  j, e" K8 m0 ~
    117.                 return Latitude;$ z0 W$ p! j& F
    118.         }( n  F6 G9 i. x
    119.         public void setLatitude(String latitude) {
      $ ?. K" J3 f- Z- Y+ [9 I" A
    120.                 Latitude = latitude;
      - O, s- _8 F& W
    121.         }3 _5 f. q& @5 ?8 T0 f- J
    122.         public String getLongitude() {
      $ k( W6 b' p+ L! X1 d3 |# W
    123.                 return Longitude;
      1 f; C  a3 M. a+ ]: g5 O
    124.         }
      $ A/ @: h$ A; ]1 F
    125.         public void setLongitude(String longitude) {
      $ o( D, s& T! A6 }
    126.                 Longitude = longitude;
      8 z, T1 ]9 v4 a# _; i  A
    127.         }8 Z  V: _$ e2 i5 ~
    128.         public String getPrecision() {! P! f, U* t8 a: ]( @+ n
    129.                 return Precision;. E$ c' N  D3 u
    130.         }
      4 @7 c: u- i" Q% h' y
    131.         public void setPrecision(String precision) {) C( W0 I" b# L
    132.                 Precision = precision;( t9 Z  _# y! |& z( C- Q7 s4 `: |" z3 I
    133.         }
      2 G& U7 M! P( l$ `* F
    134.         public String getEvent() {# ~+ z2 t0 R  T' V% _
    135.                 return Event;  F% I" [- c! X, Y# P  \3 Y+ Y9 b/ [
    136.         }6 F; x! X/ R' X# \
    137.         public void setEvent(String event) {+ }+ @! k- U1 i& I5 n* [7 s% u
    138.                 Event = event;
      & @! \& `, }/ z3 \! i6 p
    139.         }
      7 R( N. J# @6 k# ^) P- _1 [( Q8 k
    140.         public String getMsgId() {6 g+ e( V; t! ^! L# _
    141.                 return MsgId;! h' N; k8 g! o/ C
    142.         }
      1 d3 S" `  ~9 G- Z; {8 k# x& v, W
    143.         public void setMsgId(String msgId) {
      ) o3 Z$ F* X2 F
    144.                 MsgId = msgId;% K& O  V) n+ ^) ^, k
    145.         }
      ( N, a2 U8 m7 k. o9 K/ H; |# v( e
    146.         public String getToUserName() {
      + r' I: n8 S7 B1 m3 p; Z  ]. y
    147.                 return ToUserName;
      , c1 _3 F' K1 V- w
    148.         }/ [7 B1 Z- i; \8 S4 g
    149.         public void setToUserName(String toUserName) {
      5 m, e$ ~& A5 }3 X+ I2 C( [2 ]
    150.                 ToUserName = toUserName;
      + Y. X6 ?/ a3 _$ }
    151.         }
      - d( ?' y) N  B' A" k1 Z; M
    152.         public String getFromUserName() {
      5 e7 j0 @* }! X0 S5 e/ j( M+ {4 ^" T
    153.                 return FromUserName;
      ) b9 N9 ^% r3 @0 n& x' q: B
    154.         }
      $ k, e) d2 M5 a
    155.         public void setFromUserName(String fromUserName) {
      & @" i& l0 W1 l+ U2 E) A2 W1 n
    156.                 FromUserName = fromUserName;( ]" I8 t8 t" E8 [+ E3 v, B
    157.         }
      ( ^5 _' q2 |6 T1 S  C7 Z) W" f
    158.         public String getCreateTime() {
      : ^" f0 G9 G8 N1 R4 r, u0 _
    159.                 return CreateTime;
      4 ?0 a* B  ]5 k6 d; f, N
    160.         }: G' z4 M$ j% b
    161.         public void setCreateTime(String createTime) {+ }9 k" x0 y! c
    162.                 CreateTime = createTime;
      4 Y+ @1 O6 p# r& j1 a! B
    163.         }
      # U6 p! g' Y: y8 F" f% ?
    164.         public String getMsgType() {' _" ]6 v- |: p) A0 Q1 z
    165.                 return MsgType;
      8 z2 v$ \. L2 l; c1 R
    166.         }( U: J: d% z/ D- j, W, O& X
    167.         public void setMsgType(String msgType) {
      $ n; p/ D! K7 c2 M2 |3 X; Z
    168.                 MsgType = msgType;1 ^1 A8 D8 g1 _) f9 Q' t
    169.         }
      5 o0 e, f# ^" p. S" O
    170. }3 Y( o" J5 Q) `
    复制代码
    调用图灵机器人api接口,获取智能回复内容 TulingApiProcess.java
    ! }" T) }% p3 a: v" d( O& m& Z0 R. S' {& I% Q" Z  E5 P
    1. package demo.process;9 j# E1 o# j6 I& z
    2. 4 F5 s1 s% E4 Z& U, P
    3. import java.io.IOException;3 `( h) p% h' X* x$ ~# v: Z; m
    4. import java.io.UnsupportedEncodingException;' I8 n$ }& J6 g# q: M& c
    5. import java.net.URLEncoder;- i+ _9 z. i1 t
    6. 7 |2 A8 h$ V, Y
    7. import org.apache.http.HttpResponse;/ c) U: o% W- u2 n# _$ ]
    8. import org.apache.http.client.ClientProtocolException;- |5 y& a( C% p" B/ e' M7 o
    9. import org.apache.http.client.methods.HttpGet;+ P! _2 |0 z. t6 ~4 z
    10. import org.apache.http.impl.client.HttpClients;
      # Q& s1 i% Q7 f0 n
    11. import org.apache.http.util.EntityUtils;2 p0 z4 ?% d# i  q% b
    12. import org.json.JSONException;% z' X- U4 n+ {5 L1 t
    13. import org.json.JSONObject;, t7 ~; G6 p7 ]2 d. |. _

    14.   S+ S0 j6 {# G
    15. /**) c% R* E: @4 P5 |4 p2 N6 M4 K
    16. * 调用图灵机器人api接口,获取智能回复内容2 l, Q) M+ z' e+ C5 O( @; d; d
    17. * @author 科帮网! a! r7 l) j! |# L: P0 R2 F, A1 B
    18. *" K6 a% @2 |, K7 p6 e! ]. a
    19. */- S) ]1 c$ P% E1 A9 k
    20. public class TulingApiProcess {
      1 j% S3 R! x5 h
    21.         /**
      * I+ B# o9 S$ ]
    22.          * 调用图灵机器人api接口,获取智能回复内容,解析获取自己所需结果! T  G9 a8 w5 @
    23.          * @param content
      ( a+ O6 N* g0 Q2 }! G4 Z4 T
    24.          * @return! O8 }4 c. i$ Q; a
    25.          */: _. S+ l. {- U2 f4 S
    26.         public String getTulingResult(String content){
      3 |, m! g( L, y+ @# h  C' o
    27.                 /** 此处为图灵api接口,参数key需要自己去注册申请 */
      ! \; i6 C, B4 W* Z9 u& b& u
    28.                 String apiUrl = "http://www.tuling123.com/openapi/api?key=2a31b2f601f74b54ea13db1c82fe5d71&info=";( b. v9 v1 J1 j8 G
    29.                 String param = "";
      ( f. k0 o/ w+ A7 l9 _
    30.                 try {1 ?' U" n' U0 U% a" o5 u/ V6 e
    31.                         param = apiUrl+URLEncoder.encode(content,"utf-8");! ?8 [9 Q0 }- H: ~% H$ H3 A8 C2 \
    32.                 } catch (UnsupportedEncodingException e1) {
        K! b: f: _& V
    33.                         e1.printStackTrace();! ^* B/ s- W' f9 m0 d( C, e
    34.                 } //将参数转为url编码
      9 J# J5 V  ?1 e& c3 B+ d) j, e
    35.                 /** 发送httpget请求 */
      ) g& c  U: }: O% @/ ^' O7 |5 t
    36.                 HttpGet request = new HttpGet(param);
      6 z, s' v& Z* ~; o" ^' J- K
    37.                 String result = "";
      : P  v5 J5 E' R2 {
    38.                 try {
      ! J6 J& e8 x5 x- Q4 _
    39.                         HttpResponse response = HttpClients.createDefault().execute(request);
      5 O' N7 T3 @/ d, W- s  q
    40.                         if(response.getStatusLine().getStatusCode()==200){6 Y8 Y, }$ M1 @" t
    41.                                 result = EntityUtils.toString(response.getEntity());
      0 O) V# X3 J. z  J" V) a
    42.                         }
      ; U. e' u$ E: [5 ?0 i
    43.                 } catch (ClientProtocolException e) {1 G4 T9 f* m, F) _2 y! h
    44.                         e.printStackTrace();7 E$ a: N1 o: A7 g0 m, r
    45.                 } catch (IOException e) {7 {. Z- l$ s% {; L  C, ]% t4 a6 b
    46.                         e.printStackTrace();
      ) k+ ~( b# f) E0 e, r
    47.                 }6 ^1 v. p+ q8 R, u
    48.                 /** 请求失败处理 */
      ; M2 L' |3 F. D) f; Z
    49.                 if(null==result){
      / k3 q; k: M5 s# {! q
    50.                         return "对不起,你说的话真是太高深了……";
      3 v7 H. A, M5 p0 E: X! q' q4 C
    51.                 }/ _0 w9 N6 a5 A9 w) w, z6 p, I
    52.                
      5 {" |' G, A$ i1 t2 W
    53.                 try {
      7 g3 t5 n# _/ @; I5 ~. i# D
    54.                         JSONObject json = new JSONObject(result);- W7 f9 V3 C, A) M5 A" m2 R
    55.                         //以code=100000为例,参考图灵机器人api文档
      / h! [4 i) i' w  Z' t
    56.                         if(100000==json.getInt("code")){1 h' ]9 g$ e2 M+ I! p' S  Z: j& ]5 y" X
    57.                                 result = json.getString("text");5 h6 I1 A4 H+ {0 i# n3 o" P' T
    58.                         }; S. @5 s1 k( r, N9 A3 j: j
    59.                 } catch (JSONException e) {
      % |# G0 d- p# k
    60.                         e.printStackTrace();1 u9 q! v$ ^2 D& [) n% h7 Z
    61.                 }5 f: f; O; _( w4 `9 x* S
    62.                 return result;% y1 J: j# |5 a8 D1 Q
    63.         }
      0 n+ H. ^4 r% e. o
    64. }$ X. Y2 \; d+ p- V! F$ y7 \/ Y
    复制代码

      i$ ]" P- m5 w- z: S' @2 j, F$ h, tweb.xml 配置:
    + [! X) E' B" b6 W! y( V; r+ h
    1. <?xml version="1.0" encoding="UTF-8"?>
      7 u. D4 O) I8 N" R7 G+ R% Y
    2. <web-app version="2.5" % A% F  `8 v# h% J7 c$ l
    3.         xmlns="http://java.sun.com/xml/ns/javaee" 5 e9 J8 o* u. e( F
    4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      $ @; ?  d2 i# Z" _* V- h
    5.         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      8 s. Q( p7 a+ t% |6 n
    6.         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      & @2 P! J1 X7 D, N3 @
    7.   <servlet>3 _5 w! q( N* ?+ U9 ^
    8.     <servlet-name>WechatServlet</servlet-name>7 Y! r, Z4 o% K4 U0 |
    9.     <servlet-class>demo.servlet.WechatServlet</servlet-class>
      5 ?  ^: O% N/ F& Y) f
    10.   </servlet>% g5 S1 E1 R% I" _0 ~& I

    11. ) P2 F6 B7 a. @- g3 f
    12.   <servlet-mapping>5 c6 X- X" B, ~( J1 ]" f
    13.     <servlet-name>WechatServlet</servlet-name>0 K+ @& k3 j4 }8 V
    14.     <url-pattern>/wechat.do</url-pattern>
      # Y0 J+ A/ {% ^$ D4 Z& B
    15.   </servlet-mapping>
      3 N% g  J7 b+ p# S* Q. T; \
    16.   <welcome-file-list>0 j( f9 U! l2 A4 p  H
    17.     <welcome-file>index.jsp</welcome-file>+ B1 I7 I8 F9 O! O
    18.   </welcome-file-list>
      0 c% }! Q5 o1 Z  z$ i
    19. </web-app>1 a& e( Z3 }# Z* A, t
    复制代码
      L5 e" k- Q+ r
    项目源码下载地址:6 L- S( {7 X2 w3 {* P; L) L- g# q$ N
    游客,如果您要查看本帖隐藏内容请回复
    4 a/ e0 v* M! R" j7 i+ C

    8 H0 d  w  a- N7 G: Y( e" u

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


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

    红红火火恍恍惚惚

    2

    主题

    0

    听众

    204

    金钱

    三袋弟子

    该用户从未签到

    180#
    发表于 2020-02-28 14:16:34 |只看该作者
    kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
    回复

    使用道具 举报

    曾rz    

    0

    主题

    0

    听众

    92

    金钱

    三袋弟子

    该用户从未签到

    179#
    发表于 2019-09-28 19:49:14 |只看该作者
    来看看。。。。。。。。。。。。。。。。。
    回复

    使用道具 举报

    11

    主题

    0

    听众

    303

    金钱

    三袋弟子

  • TA的每日心情
    难过
    2021-3-2 20:38
  • 签到天数: 5 天

    [LV.2]偶尔看看I

    178#
    发表于 2019-09-27 01:03:54 |只看该作者
    这是什么呢?java开发微信小程序?还是模仿微信小程序?
    回复

    使用道具 举报

    AYXYJ    

    12

    主题

    1

    听众

    250

    金钱

    三袋弟子

  • TA的每日心情

    2019-5-31 15:55
  • 签到天数: 6 天

    [LV.2]偶尔看看I

    177#
    发表于 2019-04-26 18:08:00 |只看该作者
    开心,学习了
    回复

    使用道具 举报

    12

    主题

    0

    听众

    242

    金钱

    三袋弟子

  • TA的每日心情
    开心
    2019-4-20 22:11
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    176#
    发表于 2018-12-26 21:40:49 |只看该作者
    这个全吗?我也最近在学习微信,先下载下来看看,谢谢分享4 r'y3
    回复

    使用道具 举报

    4

    主题

    1

    听众

    570

    金钱

    四袋长老

  • TA的每日心情

    2019-3-10 11:07
  • 签到天数: 2 天

    [LV.1]初来乍到

    90后

    175#
    发表于 2018-09-15 16:34:10 |只看该作者
    谢谢分享.................
    回复

    使用道具 举报

    0

    主题

    0

    听众

    75

    金钱

    二袋弟子

    该用户从未签到

    174#
    发表于 2018-08-25 14:33:34 |只看该作者
    大锅饭大锅饭细化分工细化规范化风格
    回复

    使用道具 举报

    4

    主题

    1

    听众

    570

    金钱

    四袋长老

  • TA的每日心情

    2019-3-10 11:07
  • 签到天数: 2 天

    [LV.1]初来乍到

    90后

    173#
    发表于 2018-05-21 18:17:21 |只看该作者
    Java实现微信公众平台开发项目源码
    回复

    使用道具 举报

    0

    主题

    1

    听众

    87

    金钱

    三袋弟子

    该用户从未签到

    172#
    发表于 2018-05-05 23:16:14 |只看该作者
    好东西 收下了. 有帮助哦。
    回复

    使用道具 举报

    0

    主题

    0

    听众

    75

    金钱

    二袋弟子

    该用户从未签到

    171#
    发表于 2018-04-23 22:56:52 |只看该作者

    ; ?/ W2 k- o# Q# w# z! D好东西 收下了 哈哈111111111111
    回复

    使用道具 举报

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

       

    关闭

    站长推荐上一条 /1 下一条

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