Android 这个代码会不稳定吗?
我知道这是一个奇怪的问题,如果没有服务器代码,很难回答,但是可能有人发现了一个不懂语言的地方,或者有一个问题,可能是什么错了/不是通常会怎么做。 问题是:我想登录到我们的服务器(我们的服务器开发人员正在休假,所以我不能向他索要源代码),不幸的是,我总是收到一个400er错误请求错误“unsupported\u bearer\u type”。就像每30次一样(这就是为什么我问它是否可以不稳定)。从Swagger和我的应用程序的iOS版本中,我知道服务器没有任何错误,所以一定是android代码。 因此,我的问题是,这段代码是否可能不稳定或怪异Android 这个代码会不稳定吗?,android,rest,login,Android,Rest,Login,我知道这是一个奇怪的问题,如果没有服务器代码,很难回答,但是可能有人发现了一个不懂语言的地方,或者有一个问题,可能是什么错了/不是通常会怎么做。 问题是:我想登录到我们的服务器(我们的服务器开发人员正在休假,所以我不能向他索要源代码),不幸的是,我总是收到一个400er错误请求错误“unsupported\u bearer\u 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();
}
}
请将此问题移至
CodeReview
section@KevinWallis代码审阅只接受工作代码,由于此代码不能按预期工作,因此此处没有主题。@syb0rg好的,谢谢您的建议请将此问题移至CodeReview
section@KevinWallis代码审查只接受工作代码,由于这段代码不能按预期工作,所以在那里它是离题的。@syb0rg好的,谢谢你的建议