網易雲信(創建賬號,添加好友,獲取好友關系,發送系統消息《推送》,刪除好友,修改用戶信息)


    //創建網易雲通信ID
    public Netease getNeteaseToken(UserPatient userPraientNew) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/nimserver/user/create.action";
        HttpPost httpPost = new HttpPost(url);
        String accid = neteaseConfig.getAppKey();
        String appSecret = neteaseConfig.getAppSecret();
        String nonce = neteaseConfig.getNonce();
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//參考 計算CheckSum的java代碼
        // 設置請求的header
        httpPost.addHeader("AppKey", accid);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 設置請求的參數
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("accid", DateUtils.formatDateToNumberStr(new Date())));
        nvps.add(new BasicNameValuePair("name", userPraientNew.getUserName()));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        // 執行請求
        HttpResponse response = httpClient.execute(httpPost);
        // 打印執行結果
        String json = EntityUtils.toString(response.getEntity(), "utf-8");
        JSONObject str = StringUtils.jsonToStrName(json, "info");
        Netease netease = new Netease();
        netease.setAccid(str.getString("accid"));
        netease.setName(str.getString("name"));
        netease.setToken(str.getString("token"));
        // 打印執行結果
        return netease;
    }

    //獲取好友關系
    public List<DoctorBean> getAction(String accid, Long updateTime) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/nimserver/friend/get.action";
        HttpPost httpPost = new HttpPost(url);
        String appKey = neteaseConfig.getAppKey();
        String appSecret = neteaseConfig.getAppSecret();
        String nonce = neteaseConfig.getNonce();
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//參考 計算CheckSum的java代碼
        // 設置請求的header
        httpPost.addHeader("AppKey", appKey);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 設置請求的參數
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("accid", accid));
        nvps.add(new BasicNameValuePair("updatetime", String.valueOf(updateTime)));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        // 執行請求
        HttpResponse response = httpClient.execute(httpPost);
        // 打印執行結果
        String json = EntityUtils.toString(response.getEntity(), "utf-8");
        ResultBean resultBean = (ResultBean) JSONToObject(json, ResultBean.class);
        // 打印執行結果
        return resultBean.getFriends();
    }

    //添加好友
    public String add(String accid, String fAccid) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/nimserver/friend/add.action";
        HttpPost httpPost = new HttpPost(url);
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String appKey = neteaseConfig.getAppKey();
        String appSecret = neteaseConfig.getAppSecret();
        String nonce = neteaseConfig.getNonce();
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//參考 計算CheckSum的java代碼
        // 設置請求的header
        httpPost.addHeader("AppKey", appKey);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 設置請求的參數
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("accid", accid));
        nvps.add(new BasicNameValuePair("faccid", fAccid));
        // type = 1  直接添加成功
        nvps.add(new BasicNameValuePair("type", "1"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        // 執行請求
        HttpResponse response = httpClient.execute(httpPost);
        // 打印執行結果
        String json = EntityUtils.toString(response.getEntity(), "utf-8");
        return json;
    }

   /**
   *修改好友信息(備注)
   * 未完善  
   */ public String update() throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); String url = "https://api.netease.im/nimserver/friend/update.action"; HttpPost httpPost = new HttpPost(url); String accid = neteaseConfig.getAppKey(); String appSecret = neteaseConfig.getAppSecret(); String nonce = neteaseConfig.getNonce(); String curTime = String.valueOf((new Date()).getTime() / 1000L); String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//參考 計算CheckSum的java代碼 // 設置請求的header httpPost.addHeader("AppKey", accid); httpPost.addHeader("Nonce", nonce); httpPost.addHeader("CurTime", curTime); httpPost.addHeader("CheckSum", checkSum); httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // 設置請求的參數 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("accid", "25105112")); nvps.add(new BasicNameValuePair("faccid", "27154329")); nvps.add(new BasicNameValuePair("alias", "ZERO")); httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); // 執行請求 HttpResponse response = httpClient.execute(httpPost); // 打印執行結果 String json = EntityUtils.toString(response.getEntity(), "utf-8"); return json; } /**
   *修改用戶信息
   */   public String updateUinfo(UserPatient userPatient) throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); String url = "https://api.netease.im/nimserver/user/updateUinfo.action"; HttpPost httpPost = new HttpPost(url); String accid = neteaseConfig.getAppKey(); String appSecret = neteaseConfig.getAppSecret(); String nonce = neteaseConfig.getNonce(); String curTime = String.valueOf((new Date()).getTime() / 1000L); String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//參考 計算CheckSum的java代碼 // 設置請求的header httpPost.addHeader("AppKey", accid); httpPost.addHeader("Nonce", nonce); httpPost.addHeader("CurTime", curTime); httpPost.addHeader("CheckSum", checkSum); httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // 設置請求的參數 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("accid", userPatient.getNeteaseAccid())); nvps.add(new BasicNameValuePair("name", userPatient.getUserName())); httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); // 執行請求 HttpResponse response = httpClient.execute(httpPost); // 打印執行結果 String json = EntityUtils.toString(response.getEntity(), "utf-8"); return json; } /**
   *刪除好友
   */    public String del(String accid, String fAccid) throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); String url = "https://api.netease.im/nimserver/friend/delete.action"; HttpPost httpPost = new HttpPost(url); String appKey = neteaseConfig.getAppKey(); String appSecret = neteaseConfig.getAppSecret(); String nonce = neteaseConfig.getNonce(); String curTime = String.valueOf((new Date()).getTime() / 1000L); String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//參考 計算CheckSum的java代碼 // 設置請求的header httpPost.addHeader("AppKey", appKey); httpPost.addHeader("Nonce", nonce); httpPost.addHeader("CurTime", curTime); httpPost.addHeader("CheckSum", checkSum); httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // 設置請求的參數 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("accid", accid)); nvps.add(new BasicNameValuePair("faccid", fAccid)); httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); // 執行請求 HttpResponse response = httpClient.execute(httpPost); // 打印執行結果 String json = EntityUtils.toString(response.getEntity(), "utf-8"); return json; } /**
   *發送系統消息
   */ public String sendMsg(String accid, String faccid, String jsonStr) throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); String url = "https://api.netease.im/nimserver/msg/sendAttachMsg.action"; HttpPost httpPost = new HttpPost(url); String appKey = neteaseConfig.getAppKey(); String nonce = neteaseConfig.getNonce(); String appSecret = neteaseConfig.getAppSecret(); ; String curTime = String.valueOf((new Date()).getTime() / 1000L); String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//參考 計算CheckSum的java代碼 // 設置請求的header httpPost.addHeader("Nonce", nonce); httpPost.addHeader("CurTime", curTime); httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); httpPost.addHeader("CheckSum", checkSum); httpPost.addHeader("AppKey", appKey); // 設置請求的參數 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("accid", accid)); nvps.add(new BasicNameValuePair("msgtype", "0")); nvps.add(new BasicNameValuePair("to", faccid)); nvps.add(new BasicNameValuePair("body", jsonStr)); httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); // 執行請求 HttpResponse response = httpClient.execute(httpPost); // 打印執行結果 String json = EntityUtils.toString(response.getEntity(), "utf-8"); return json; }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM