我正在尝试使用谷歌URL缩短器API和Volley,我不断收到响应BasicNetwork.performRequest:意外的响应代码400.我正在使用POST请求来缩短网址。我在同一视图上的其他POST也正常工作。我已经尝试了所有不同类型的配置而没有运气,这适用于我的iOS应用程序。知道我在这里做错了吗?我的LogCat几乎没有给我任何东西。代码在这里:
public void ShortenURL() {
String endpoint = "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyA32UQwEvxZNPClTsjZWzm6wO1xNGlkH-U";
final String toShorten = mShortenEditText.getText().toString();
RequestQueue queue = Volley.newRequestQueue(getActivity());
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST, endpoint,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//messageContentEditText.setText("");
Toast.makeText(getActivity(), "URL Shortened", Toast.LENGTH_LONG).show();
//parse response and display new URL in edittext
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
error.printStackTrace();
System.out.println(error.networkResponse.toString());
}
}) {
//adding parameters to the request
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("longUrl", "http://wwww.espn.com/newyorkjets/joshmccown");
//params.put("key", "AIzaSyA32UQwEvxZNPClTsjZWzm6wO1xNGlkH-U");
return params;
}
@Override
public Map<String, String> getHeaders () throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
//headers.put("Accept","application/json");
return headers;
}
};
queue.add(stringRequest);
}
logcat的:
11-16 11:56:47.643 1446-1658/com.a2.reach1 D/NetworkSecurityConfig: No Network Security Config specified, using platform default
11-16 11:56:48.710 1446-1658/com.a2.reach1 E/Volley: [321] BasicNetwork.performRequest: Unexpected response code 400 for https://www.googleapis.com/urlshortener/v1/url
11-16 11:56:48.717 1446-1446/com.a2.reach1 W/System.err: com.android.volley.ServerError
11-16 11:56:48.717 1446-1446/com.a2.reach1 W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
11-16 11:56:48.717 1446-1446/com.a2.reach1 W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
11-16 11:56:48.717 1446-1446/com.a2.reach1 I/System.out: com.android.volley.NetworkResponse@df41f55
11-16 11:56:48.802 1446-1503/com.a2.reach1 D/EGL_emulation: eglMakeCurrent: 0xa12fcf60: ver 2 0 (tinfo 0xa12f9670)
11-16 11:56:48.917 1446-1503/com.a2.reach1 D/EGL_emulation: eglMakeCurrent: 0xa12fcf60: ver 2 0 (tinfo 0xa12f9670)
11-16 11:57:12.221 1446-1453/com.a2.reach1 W/art: Suspending all threads took: 7.226ms
这是来自Hurl.it
Alt-Svc: hq=":443"; ma=2592000; quic=51303431; quic=51303339; quic=51303338; quic=51303337; quic=51303335,quic=":443"; ma=2592000; v="41,39,38,37,35"
Cache-Control: private, max-age=0
Content-Encoding: gzip
Content-Type: application/json; charset=UTF-8
Date: Fri, 17 Nov 2017 04:15:06 GMT
Expires: Fri, 17 Nov 2017 04:15:06 GMT
Server: GSE
Transfer-Encoding: chunked
Vary: Origin, X-Origin
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}