我知道这是一个奇怪的问题,如果没有服务器代码很难回答,但也许有人发现了一个不正常的问题,或者有一个猜测是什么可能是错的/不是通常会怎么做。 问题是:我想登录我们的服务器(我们的服务器开发人员正在接种疫苗,因此我无法向他询问源代码),不幸的是我总是得到400er错误的请求错误" unsupported_bearer_type"。就像它第30次一样(这就是为什么我会问它是否可能不稳定)。从Swagger和我的应用程序的iOS版本我知道,服务器没有任何错误,所以它必须是Android代码。 因此我的问题是,如果这段代码可能不稳定或奇怪?:
URL urlToRequest = null;
HttpURLConnection urlConnection = null;
try {
urlToRequest = new URL(mUrlString);
urlConnection =
(HttpURLConnection) urlToRequest.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
String postParameters ="grant_type=password&username=" + email + "&password=" + pw;
try {
postParameters = URLEncoder.encode(postParameters, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
urlConnection.setFixedLengthStreamingMode(
postParameters.getBytes().length);
PrintWriter out = new PrintWriter(urlConnection.getOutputStream());
out.print(postParameters);
out.close();
int status = urlConnection.getResponseCode();
InputStreamReader in = new InputStreamReader(urlConnection.getInputStream());
BufferedReader rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line = "";
String ObjectInString = "";
while ((line = rd.readLine()) != null) {
ObjectInString += line;
}
JSONObject jsonObj = new JSONObject(ObjectInString);
_id = jsonObj.getString("Id");
String AuthToken = jsonObj.getString("access_token");
String AuthTokenExpireDate = jsonObj.getString(".expires");
String RefreshToken = jsonObj.getString("refresh_token");
...
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}