android连接融云获取token报错 1C

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个回答

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐