异常 没有apikey
response>﹕ {"errNum":300202,"errMsg":"Missing apikey"}
apikey 需放到header
imageRequestQueue 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<JSONObject>() {
@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<String, String> headers = new HashMap<String, String>();
public MyStringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
}
@Override
public Map<String, String> 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<String>() {
@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"}}
网友评论