以下URLConnection Post任务在Android 8中运行良好,但在Android 9中间歇性运行。 例如,它在前两个调用中返回错误代码500,然后在第3个调用中,从后端响应中返回带有令牌参数的良好代码200。其他时候,它是在Android 9模拟器中编译并重新启动我的应用程序后立即尝试运行的,在多次调用中运行良好,然后开始返回有缺陷的错误Code 500。 您会感到...可靠性大约是50/50。 我认为不是模拟器,因为一位同事在物理Android 9 Pie设备上测试了Apk,并且发生了相同的行为。 我还相信我的输入参数字符串是正确的,因为身份验证至少可以工作几次,因此该错误必须在我的代码上的某些实现上实现。
有什么想法吗?
这是代码:
LoginPIEAsync lp = new LoginPIEAsync(context, params, targetURL, jParams); //L391
lp.execute();
public static class LoginPIEAsync extends AsyncTask<String, Void, String> {
Context context;
String dataString;
byte[] bodyData;
URL url = null;
String user, pwd, details, packagename;
@RequiresApi(api = Build.VERSION_CODES.KITKAT) //Constructor
public LoginPIEAsync(Context ctx, String params, URL url, JSONObject j) throws UnsupportedEncodingException {
this.context = ctx;
this.bodyData = params.getBytes(HTTP.UTF_8);
this.dataString = params;
this.url = url;
loge("ASYNC URL: " + this.url.toString());
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected String doInBackground(String... params) {
String response;
InputStream stream = null;
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Accept-Charset", "UTF-8;q=1;ISO-8859-1");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("charset", "utf-8");
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Content-Length", Integer.toString(this.bodyData.length));
urlConnection.setRequestProperty("Accept-Encoding", HTTP.UTF_8);
//urlConnection.setUseCaches (false);
urlConnection.setConnectTimeout(19000);
urlConnection.setReadTimeout(19000);
//urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
String baseAuthStr = ANDROID_CONSUMER_KEY + ":" + ANDROID_CONSUMER_SECRET;
urlConnection.addRequestProperty("Authorization", "Basic " + baseAuthStr);
OutputStream out = urlConnection.getOutputStream();
out.write(bodyData);
out.flush ();
out.close();
urlConnection.connect();
InputStream inputStream;
int status = urlConnection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
inputStream = urlConnection.getErrorStream();
}
else {
inputStream = urlConnection.getInputStream();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String inputLine;
while ((inputLine = reader.readLine()) != null) {
loge("inputSTREAM: " + inputLine);
result+=inputLine;
}
reader.close();
loge("PIE CONNECTION, getResponseCode: "+String.valueOf(status));
loge("PIE CONNECTION, result: "+result);
return result;
} catch (IOException e) {
loge("IOException: "+e.toString());
e.printStackTrace();
} finally {
if (urlConnection != null) {
loge("DISCONNECTING.....");
urlConnection.disconnect();
}
}
try {
Thread.sleep(25000);
} catch (InterruptedException e) {
e.printStackTrace();
loge("Result, SLEEP ERROR");
}
return null;
}
@Override
protected void onPostExecute(String result) {
if(result==null){
Toast toast = Toast.makeText(context, "Token Failed", Toast.LENGTH_LONG);
toast.show();
}
else{
Toast toast = Toast.makeText(context, "Token: "+result, Toast.LENGTH_LONG);
toast.show();
}
loge("ASYNC onPostExecute: "+result);
}
}