我正在尝试从 Directus Api 获取 JSON 数据,并且从浏览器发出请求时工作正常。但是,当我从我的 android 应用程序发送相同的请求(相同的 URL,带有“授权”标头或带有参数)时 - 请求失败并出现 java.io.FileNotFoundException和响应代码 405。
带参数的 URL:在浏览器中工作
http://example.com/dev/public/_/items/cities/1?access_token=my_test_token
带有参数的相同请求:FAILS IN ADNROID
Uri.Builder builder = new Uri.Builder();
builder.appendQueryParameter("access_token","my_test_token");
//..
URL url = new URL(serverURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("Content-Type", "application/json; UTF-8");
conn.addRequestProperty("Accept","application/json; charset=utf-8");
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
不带参数的 URL,带有标头“授权:承载 my_test_token”:在浏览器中工作
http://example.com/dev/public/_/items/cities/1
带有“授权:承载 my_test_token”标头的相同请求:ANDROID 中的失败
URL url = new URL(serverURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("Authorization", "Bearer " + token);
conn.addRequestProperty("Content-Type", "application/json; UTF-8");
conn.addRequestProperty("Accept","application/json; charset=utf-8");
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
Non of the above requests (with parameters or with 'Authorization') work in Android. Is there any difference between browser and Android requests? Why the same request fails on Android giving java.io.FileNotFoundException but works in the browser? Can anyone help? Thanks.