0

以下 URLConnection Post 任务在 Android 8 中运行良好,但在 Android 9 中运行时断时续。例如,它在前两次调用时返回错误代码 500,然后在第三次调用时,它返回良好的代码 200,并带有来自后端的令牌参数回复。其他时候,在 Android 9 模拟器中编译并重新启动我的应用程序后,它在第一次尝试时就可以工作,在多次调用期间运行良好,然后开始返回有缺陷的错误代码 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);
        }
}
4

0 回答 0