如何将给定的json包装到字符串并通过Http put请求将其发送到服务器?
这就是我的json的样子.
{
"version": "1.0.0",
"datastreams": [
{
"id": "example",
"current_value": "333"
},
{
"id": "key",
"current_value": "value"
},
{
"id": "datastream",
"current_value": "1337"
}
]
}
Run Code Online (Sandbox Code Playgroud)
上面是我的json数组.
下面是我编写代码的方法,但它不起作用
protected String doInBackground(Void... params) {
String text = null;
try {
JSONObject child1 = new JSONObject();
try{
child1.put("id", "LED");
child1.put("current_value", "0");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONArray jsonArray = new JSONArray();
jsonArray.put(child1);
JSONObject datastreams = new JSONObject();
datastreams.put("datastreams", jsonArray);
JSONObject version = new …Run Code Online (Sandbox Code Playgroud) 我试图从产品密码和设备序列号计算激活码,我计算的激活码总是与Xively不同.
有人可以帮我指出我错在哪里吗?
来自Xievely门户网站:
Product ID: 4sA5tK9XF_3xKfOawmyO
Product Secret: be0f6928e3653cf175b7f8ebb2da50c6658b30b7
Serial Number: 123123123
Activation Code: 93d80b284a19d14e99d9abb7d637afc59d4b0f95
Run Code Online (Sandbox Code Playgroud)
我用PHP测试过:
echo hash_hmac("sha1","123123123","be0f6928e3653cf175b7f8ebb2da50c6658b30b7");
Run Code Online (Sandbox Code Playgroud)
我生成的激活码是:66b02f4c691287144c09e3b76816275742c155b5,它与Xively不同.
请帮忙,万分感谢.
我正在尝试使用他们的MQTT服务器将我的Arduino与以太网连接到Xively但是我在验证时遇到了麻烦.当我这样做时,client.connect("arduinoMQTT")我失败了.我尝试将API密钥作为用户名传递,client.connect("arduinoMQTT", "APIKEY")但代码将无法编译.
如何在arduino上对Xively MQTT服务器进行身份验证.
我有一个arduino上传传感器数据到cosm.com.我在我的本地Web服务器上创建了一个简单的网页来查询cosm.com API并打印出值.
问题是如果我没有在另一个标签中登录cosm.com,我会得到这个弹出窗口.

解决方案是将我的公钥传递给cosm.com,但我在这里已经超出了我的想法.
该文档提供了如何在curl中执行此操作的示例,但不是javascript
curl --request GET --header "X-ApiKey: -Ux_JTwgP-8pje981acMa5811-mSAKxpR3VRUHRFQ3RBUT0g" https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading
Run Code Online (Sandbox Code Playgroud)
如何将我的密钥传递到网址?:
function getJson() {
$.ajax({
type:'GET',
url:"https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading",
//This line isn't working
data:"X-ApiKey: -Ux_JTwgP-8pje981acMa5811-mSAKxpR3VRUHRFQ3RBUT0g",
success:function(feed) {
var currentSensorValue = feed.current_value;
$('#rawData').html( currentSensorValue );
},
dataType:'jsonp'
});
}
Run Code Online (Sandbox Code Playgroud)
更新: 必须是可能的,因为hurl.it能够查询api http://www.hurl.it/hurls/75502ac851ebc7e195aa26c62718f58fecc4a341/47ad3b36639001c3a663e716ccdf3840352645f1
更新2: 虽然我从来没有让这个工作,我确实找到了解决方法.Cosm有自己的javascript库,可以满足我的需求.
http://cosm.github.com/cosm-js/ http://jsfiddle.net/spuder/nvxQ2/5/