魔獸傳奇

          java程序愛好者
          posts - 28, comments - 16, trackbacks - 0, articles - 6
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          日歷

          <2015年9月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          mybatis學習

          搜索

          •  

          最新評論

          1、Web服務器是Spring MVC框架:
          package com.acce.server.order.controller;
          import javax.annotation.Resource;
          import javax.servlet.http.HttpServletResponse;
          import org.springframework.stereotype.Controller;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.RequestParam;
          import com.acce.server.core.controller.BaseController;
          import com.acce.server.pcOrder.service.PcBillService;
          import com.alibaba.fastjson.JSONObject;
          @Controller()
          public class TestController extends BaseController {
          @Resource(name = "PcBillService")
          private PcBillService pcBillService;
          /**
          * 獲取tabFirster表中屬于該用戶的信息。
          * @param json
          * @param response
          */
          @RequestMapping("/api/order/querryTabHeadByPC.html")
          public void querryTabHead(@RequestParam String json,
          HttpServletResponse response) {
          System.out.println("/api/order/querryTabHead.html 接口獲取到json:" + json);
          String rstString = pcBillService.querryTabHead(
          JSONObject.parseObject(json)).toString();
          response.setContentType("text/html; charset=utf-8");
          this.wirteJsonToResponse(response, rstString);
          }
          }

          2、將響應結果返回:
          package com.acce.server.core.controller;
          import java.io.IOException;
          import java.io.PrintWriter;
          import javax.servlet.http.HttpServletResponse;
          import org.apache.log4j.Logger;
          public class BaseController {
          protected Logger LOGGER = Logger.getLogger(this.getClass());
          /**
          * 將json寫到servlet輸出流
          * @param response
          * @param json
          */
          protected void wirteJsonToResponse(HttpServletResponse response,String json) {
          PrintWriter writer = null;
          try {
          writer = response.getWriter();
          writer.println(json);
          writer.flush();
          } catch (IOException e) {
          LOGGER.error("服務端寫json報錯:"+json,e);
          } finally {
          if(null != writer) {
          writer.close();
          }
          }
          }
          }
          3、新起一個JavaProject,導入HttpClient相關jar包:httpclient-4.3.6.jar、httpclient-cache-4.2.jar、httpcore-4.3.3.jar、httpcore-ab-4.2.1.jar、httpcore-nio-4.2.1.jar、httpmime-4.2.jar
          package acce;
          import java.io.BufferedReader;
          import java.io.InputStreamReader;
          import java.util.ArrayList;
          import org.apache.http.HttpResponse;
          import org.apache.http.HttpStatus;
          import org.apache.http.NameValuePair;
          import org.apache.http.client.HttpClient;
          import org.apache.http.client.entity.UrlEncodedFormEntity;
          import org.apache.http.client.methods.HttpPost;
          import org.apache.http.impl.client.DefaultHttpClient;
          import org.apache.http.message.BasicNameValuePair;
          import org.apache.http.protocol.HTTP;
          public class TestHttpClient {
          public static void main(String[] args) {
          // \"{\"tabCorpFirst\":\"快來大酒店\",\"syncing\":\"1\"}\"
          String url = "http://localhost:8080/acce-server/api/order/querryKingdomFromFirster.html";
          try {
          HttpClient httpClient = new DefaultHttpClient();
          // HttpPost 實現 HttpUriRequest 接口,HttpUriRequest接口 繼承 HttpRequest
          HttpPost httpPostReq = new HttpPost(url);
          ArrayList<NameValuePair> paramList = new ArrayList<NameValuePair>();
          String json = "{\"tabCorpFirst\":\"快來大酒店\",\"syncing\":\"1\"}";
          NameValuePair param1 = new BasicNameValuePair("json", json);
          paramList.add(param1);
          UrlEncodedFormEntity aa = new UrlEncodedFormEntity(paramList,
          HTTP.UTF_8);
          httpPostReq.setEntity(aa);
          HttpResponse resp = httpClient.execute(httpPostReq);
          if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
          BufferedReader reader = new BufferedReader(
          new InputStreamReader(resp.getEntity().getContent()));
          StringBuffer result = new StringBuffer();
          String inputLine = null;
          while ((inputLine = reader.readLine()) != null) {
          result.append(inputLine);
          }
          System.out.println("結果: " + result.toString());
          } else {
          System.out.println("結果: "
          + resp.getStatusLine().getStatusCode());
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          }
          }
          測試調用接口正確,并能獲取到正確的值;
          注:由于項目是UTF-8,需要轉碼。

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 郯城县| 五常市| 泰州市| 隆安县| 巴塘县| 铜陵市| 巫山县| 贺兰县| 抚远县| 南澳县| 北海市| 丹棱县| 于田县| 定州市| 乌兰浩特市| 五指山市| 陆川县| 增城市| 长寿区| 南城县| 衢州市| 鄂伦春自治旗| 新巴尔虎右旗| 正镶白旗| 班玛县| 砚山县| 儋州市| 深州市| 丹凤县| 林口县| 沾益县| 永济市| 长阳| 钦州市| 和政县| 郯城县| 商丘市| 广州市| 桃园市| 靖边县| 张家川|