Android API授权标头值

时间:2014-08-14 18:15:02

标签: android json

为了练习和学习Android我正在做一个简单的气象应用程序,为此我使用api将设备发送到当地的天气。我必须通过Json和两个标头值将此位置发送到此API,我在api文档中有此示例:

  

HTTP方法PUT
  授权标头值:
  deviceid = 8489456AZXXXXXX,api_key = 354337637ARA3453463ZRXXXXXXX,   basicauthentication

     

请求数据示例:{latitude:123456789,经度:123456789}

所以我的问题基本上是我不知道谁使用标头值而我无法连接api,我正在尝试下面的代码,但我收到此错误:

  

“InvalidDataAccessApiUsageException”,“message”:“给定的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;

}

我做错了什么? 感谢

0 个答案:

没有答案