小周天 2017-06-07 02:41 采纳率: 0%
浏览 1688
已结题

android连接融云获取token报错

package cn.hhs.aitaodian.test;

import org.apache.http.HttpResponse;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

/**

  • Created by Administrator on 2017/6/6 0006. */

public class GetToken {
public static String GetRongCloudToken(String userID) {
StringBuffer res = new StringBuffer();
String url = "https://api.cn.ronghub.com/user/getToken.json";
String App_Key = "****"; //开发者平台分配的 App Key。
String App_Secret = "******";
String Timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳,从 1970 年 1 月 1 日 0 点 0 分 0 秒开始到现在的秒数。
String Nonce = String.valueOf(Math.floor(Math.random() * 1000000));//随机数,无长度限制。
String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("App-Key", App_Key);
httpPost.setHeader("Timestamp", Timestamp);
httpPost.setHeader("Nonce", Nonce);
httpPost.setHeader("Signature", Signature);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
List nameValuePair = new ArrayList(1);
nameValuePair.add(new BasicNameValuePair("userId",userID));
nameValuePair.add(new BasicNameValuePair("name","张三"));
nameValuePair.add(new BasicNameValuePair("portraitUri","https://ss2.bdstatic.com/8_V1bjqh_Q23odCf/pacific/upload_23068489_1486463968474.jpg"));
HttpResponse httpResponse = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"utf-8"));
httpResponse = httpClient.execute(httpPost);
BufferedReader br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String line = null;
while ((line = br.readLine()) != null) {
res.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}

   /* UserRespone userRespone = JSON.parseObject(res.toString(), UserRespone.class);
    Logger.i(userRespone.getCode()+"");
    return userRespone.getToken();*/
    return  res.substring(0,res.length());
}
//SHA1加密//http://www.rongcloud.cn/docs/server.html#通用_API_接口签名规则
private static String sha1(String data){
    StringBuffer buf = new StringBuffer();
    try{
        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(data.getBytes());
        byte[] bits = md.digest();
        for(int i = 0 ; i < bits.length;i++){
            int a = bits[i];
            if(a<0) a+=256;
            if(a<16) buf.append("0");
            buf.append(Integer.toHexString(a));
        }
    }catch(Exception e){

    }
    return buf.toString();
}

}

报错:404

  • 写回答

1条回答 默认 最新

  • viewgroup 2017-06-07 05:58
    关注

    404肯定是你的连接URL出错了,你认真检查一下API看一下请求连接

    评论

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题