Android API授权头值
为了练习和了解Android,我正在做一个简单的气象应用程序,为此我使用了一个api,将本地天气信息发送给设备。我必须通过Json和两个标题值将我的位置发送到此API,API文档中有以下示例: HTTP方法PUT 授权标头值: deviceid=8489456AZXXXXXX,api_key=354337637ARA3453463ZRXXXXXX, 基底动脉造影 请求数据示例:{纬度:123456789,经度:123456789} 因此,我的问题基本上是,我不知道谁应该使用头值,我无法连接api,我正在尝试下面的代码,但我得到了以下错误: InvalidDataAccessApiUsageException,消息:给定的id不能 无效 我做错了什么?。Android API授权头值,android,json,Android,Json,为了练习和了解Android,我正在做一个简单的气象应用程序,为此我使用了一个api,将本地天气信息发送给设备。我必须通过Json和两个标题值将我的位置发送到此API,API文档中有以下示例: HTTP方法PUT 授权标头值: deviceid=8489456AZXXXXXX,api_key=354337637ARA3453463ZRXXXXXX, 基底动脉造影 请求数据示例:{纬度:123456789,经度:123456789} 因此,我的问题基本上是,我不知道谁应该使用头值,我无法连接api
谢谢谁知道这个API是如何工作的,只有你能知道哪里出了问题。由于您至少没有链接到文档hanks@meda是我所有的文档,不知道API是如何工作的,没有人能猜到这个问题,这就是为什么您没有得到answers@meda再一次谢谢,我有来自API支持的消息,翻译或多或少地说:您必须将api密钥和设备id添加到授权头中,还需要基本身份验证才能获取本地天气
public JSONObject callApi (String url) {
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(url);
httpPut.addHeader("deviceid","8489456AZXXXXXX");
httpPut.addHeader("api_key","354337637ARA3453463ZRXXXXXXX");
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("latitude",123122312);
jsonObject.accumulate("longitude",1231321123);
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
httpPut.setEntity(se);
httpPut.setHeader("Accept", "application/json");
httpPut.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPut);
inputStream = httpResponse.getEntity().getContent();
if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
try {
jsonSent = new JSONObject(result);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jsonSent;
}