@Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

    String code = request.getParameter("code");
    String Token = (String) request.getSession().getAttribute("accessToken");
    System.out.println("accessToken" + Token);
    String requestUrl =
        "https://graph.renren.com/oauth/token?grant_type=authorization_code&client_id="
            + client_ID
            + "&redirect_uri=http://other.internetrt.org:8080/renrent/home&client_secret="
            + client_SECRET
            + "&code="
            + code;
    String result = httpClientPost(requestUrl);

    System.out.println("[HomeServlet : doPost]" + result);
    try {
      JSONObject json = JSONObject.fromString(result);
      String sessionKey = (String) json.get("access_token");
      System.out.println("[HomeServlet : doPost]: " + "renrenSessionKey: " + sessionKey);
      request.setAttribute("sessionkeyfromrenren", sessionKey);
      System.out.println("[HomeServlet : doPost]: " + "renrenSessionKey" + sessionKey);
      ApiInitListener.feedstub.addFeedUser(sessionKey, Token);
    } catch (Exception err) {
      err.printStackTrace();
      System.out.println("[HomeServlet : doPost] json串问题");
    }

    RequestDispatcher welcomeDispatcher = request.getRequestDispatcher("/views/home.jsp");
    welcomeDispatcher.forward(request, response);
  }
  private static JSONObject open(String host, int port) throws Exception {
    JSONObject result;
    PostMethod method = new PostMethod(host + ":" + port + "/api/data/store.open");

    // client.getState().setCredentials(new AuthScope(null, repoHostPort, "authentication"), new
    // UsernamePasswordCredentials(memberId, memberPassword));

    // method.setDoAuthentication(true);

    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
      System.err.println("Method failed: " + method.getStatusLine());
    }

    Reader reader = new InputStreamReader(method.getResponseBodyAsStream());
    CharArrayWriter cdata = new CharArrayWriter();
    char buf[] = new char[BUFFER_SIZE];
    int ret;
    while ((ret = reader.read(buf, 0, BUFFER_SIZE)) != -1) cdata.write(buf, 0, ret);

    reader.close();

    result = JSONObject.fromString(cdata.toString());

    method.releaseConnection();

    if (result.has("error")) throw new GeneralException(result.getString("error"));

    return result;
  }
  public JSONObject delete(String path) throws Exception {
    JSONObject result = new JSONObject();
    DeleteMethod method = new DeleteMethod(repoHostname + ":" + repoHostPort + "/api/data/" + path);
    method.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    method.setRequestHeader("Cookie", "jsonhub-store-key=" + getStoreKey());

    // client.getState().setCredentials(new AuthScope(null, repoHostPort, "authentication"), new
    // UsernamePasswordCredentials(memberId, memberPassword));

    // method.setDoAuthentication(true);

    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
      System.err.println("Method failed: " + method.getStatusLine());
    }

    Reader reader = new InputStreamReader(method.getResponseBodyAsStream());
    CharArrayWriter cdata = new CharArrayWriter();
    char buf[] = new char[BUFFER_SIZE];
    int ret;
    while ((ret = reader.read(buf, 0, BUFFER_SIZE)) != -1) cdata.write(buf, 0, ret);

    reader.close();

    result = JSONObject.fromString(cdata.toString());

    method.releaseConnection();

    if (result.has("error")) throw new GeneralException(result.getString("error"));

    return result;
  }
예제 #4
0
 /**
  * 返回默认列表的json
  *
  * @param list
  * @param server
  * @return
  */
 private String getDefaultListToJson(List list, String server) {
   String result = "[";
   String size = "25|32";
   String clickUrl = "";
   String jsonStr = "";
   JSONArray ja = new JSONArray();
   JSONObject js = null;
   for (int i = 0; i < list.size(); i++) {
     YouHotel th = (YouHotel) list.get(i);
     String addr = th.getAddress();
     if (addr != null && addr.length() > 10) {
       addr = addr.substring(0, 10) + "...";
     }
     if (StringUtils.isBlank(addr)) {
       addr = "未知";
     }
     String icon = "../../images/you/map/" + (i + 1) + ".png";
     clickUrl = server + "/pageDetailed_-1_hotel_" + th.getId() + ".html";
     jsonStr =
         "{\"point\":\""
             + th.getNewLongitude()
             + "|"
             + th.getNewLatitude()
             + "\",\"shortTitle\":\""
             + th.getName()
             + "\",\"fullTitle\":\""
             + th.getName()
             + "\",\"price\":\""
             + th.getSalesPrice()
             + "&nbsp;&nbsp;&nbsp;&nbsp;\",\"shortAddr\":\""
             + addr
             + "\",\"fullAddr\":\""
             + th.getAddress()
             + "\",\"star\":\""
             + Constants.HOTEL_INT_CHN_MAP.get(th.getLevelInfo())
             + "\",\"pic\":\""
             + th.getImgurl()
             + "\",\"icon\":\""
             + icon
             + "\",\"size\":\""
             + size
             + "\",\"url\":\""
             + clickUrl
             + "\"}";
     js = JSONObject.fromString(jsonStr);
     ja.put(js);
     result = result + jsonStr + ",";
   }
   result = result.substring(0, result.length() - 1) + "]";
   return result;
 }
 /**
  * TODO:登录接口测试
  *
  * @author 作者:邱卫武
  * @date 创建时间:2015年9月8日
  * @param url
  * @param excelPath
  * @param sheetName
  * @throws Exception void
  */
 public void login(String url, String excelPath, String sheetName, String tablename, int datanum)
     throws Exception {
   for (int i = 1; i <= datanum; i++) {
     JSONObject response =
         JSONObject.fromString(
             request.doPost(url, sqldata.getData_login(tablename, i))); // 判断登录是否成功
     if ("false".equals(data.getResultDataBy_one(response, "IsSuccess"))) {
       String message = data.getResultDataBy_one(response, "Message");
       check.checkString(message, readexcel.readExcel13_oneValue(excelPath, sheetName, i, "预期结果"));
     } else {
       report.log("登录成功");
     }
   }
 }