百度apistore

异常 没有apikey

response>﹕ {"errNum":300202,"errMsg":"Missing apikey"}

apikey 需放到header

百度apistore_第1张图片
image

RequestQueue mVolleyQueue = Volley.newRequestQueue(this);
String url = "http://apis.baidu.com/apistore/weatherservice/weather?apikey=1e08b47bc5fc83bccc9b6bfb3b4cf1df&citypinyin=hangzhou";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
Log.i("response>", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("error>", error.toString());
}
});
mVolleyQueue.add(jsonObjectRequest);

放到header

public class MyStringRequest extends StringRequest {
private Map headers = new HashMap();

    public MyStringRequest(int method, String url, Response.Listener listener, Response.ErrorListener errorListener) {
        super(method, url, listener, errorListener);
    }

    @Override
    public Map getHeaders() throws AuthFailureError {
        return headers;
    }

    public void setHeader(String title, String content) {
        headers.put(title, content);
    }
}

请求

RequestQueue mVolleyQueue = Volley.newRequestQueue(this);
String url = "http://apis.baidu.com/apistore/weatherservice/weather?citypinyin=hangzhou";
MyStringRequest myStringRequest = new MyStringRequest(Request.Method.GET, url, new Response.Listener() {
@Override
public void onResponse(String response) {
Log.i("response>", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("error>", error.toString());
}
});
myStringRequest.headers.put("apikey", "1e08b47bc5fc83bccc9b6bfb3b4cf1df");
mVolleyQueue.add(myStringRequest);

结果

{"errNum":0,"errMsg":"success","retData":{"city":"\u676d\u5dde","pinyin":"hangzhou","citycode":"101210101","date":"15-07-23","time":"11:00","postCode":"310000","longitude":120.165,"latitude":30.319,"altitude":"43","weather":"\u96f7\u9635\u96e8","temp":"32","l_tmp":"26","h_tmp":"32","WD":"\u65e0\u6301\u7eed\u98ce\u5411","WS":"\u5fae\u98ce(<10m/h)","sunrise":"05:12","sunset":"18:59"}}

你可能感兴趣的:(百度apistore)