在KITKAT android版本中从服务获取数据时应用程序变慢

时间:2014-04-03 15:05:36

标签: android android-4.4-kitkat

该应用程序在android中低于4.4.2版本时运行良好。当我在4.4.2版本中运行应用程序时,设备应用程序在从Web服务获取/调用数据时非常慢。

有人面临这种​​问题吗?如果有解决方案,请帮助我。

以下是我的attachemnt上传代码。

私有类UploadAttachmentTask扩展了AsyncTask {

    ProgressDialog dialog;
    int result = 0;

    @Override
    protected void onPostExecute(Boolean res) {
        super.onPostExecute(res);
        if (!ApplicationData.isOffline())
            dialog.dismiss();
        updateData(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (!ApplicationData.isOffline()) {
            dialog = new ProgressDialog(EvidenceDetailActivity.this);
            dialog.setMessage("Uploading resource...");
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setMax(100);
            dialog.setProgress(0);
            dialog.setCancelable(false);
            dialog.show();
        }
    }

    // posting image audio, video file to server
    public int postMediaDataToServer(String evidenceId, File file) {
        String fileName = service.createFileNameWithDate(file);
        System.gc();
        int result = postAttachmentToServer(fileName, file, evidenceId);
        return result;
    }

    @SuppressWarnings("deprecation")
    public int postAttachmentToServer(String fileName, File file,
            String evidenceId) {
        try {
            System.gc();
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;

            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "----WebKitFormBoundarysNDKBMUFqqvzYWGh";

            byte[] buffer;
            try {
                long fileSize = file.length();
                int bufferSize = 1024 * 512;
                FileInputStream fileInputStream = new FileInputStream(file);
                URL url = new URL(CacheAPI.mediaURL());


                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("POST");
                //conn.setChunkedStreamingMode(bufferSize);
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("Accept", "application/json");
                conn.setRequestProperty("Accept-Charset", "utf-8");
                conn.setRequestProperty("X-Ecordia-Software", "ecordiApp");
                List<Cookie> cookies = ApplicationData.getCookieStore()
                        .getCookies();
                if (cookies.size() > 0)
                    conn.setRequestProperty("Cookie", cookies.get(0)
                            .getName() + "=" + cookies.get(0).getValue());

                conn.setRequestProperty("Content-Type",
                        "multipart/form-data; boundary=" + boundary);
                dos = new DataOutputStream(conn.getOutputStream());

                dos.writeBytes(lineEnd + twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"EvidenceIdentifier\""
                        + lineEnd + lineEnd);
                dos.writeBytes(evidenceId + lineEnd);
                dos.writeBytes(lineEnd + twoHyphens + boundary + lineEnd);
                if (fileName.endsWith(".jpg")) {
                    dos.writeBytes("Content-Disposition: form-data; name=\"File0\"; filename=\""
                            + fileName + "\"" + lineEnd);
                    dos.writeBytes("Content-Type: image/jpeg" + lineEnd
                            + lineEnd);
                } else if (fileName.endsWith(".mp4")) {
                    dos.writeBytes("Content-Disposition: form-data; name=\"File0\"; filename=\""
                            + fileName + "\"" + lineEnd);
                    dos.writeBytes("Content-Type: video/mp4" + lineEnd
                            + lineEnd);
                } else if (fileName.endsWith(".3gp")) {
                    dos.writeBytes("Content-Disposition: form-data; name=\"File0\"; filename=\""
                            + fileName + "\"" + lineEnd);
                    dos.writeBytes("Content-Type: video/3gp" + lineEnd
                            + lineEnd);
                } else if (fileName.endsWith(".mov")) {
                    dos.writeBytes("Content-Disposition: form-data; name=\"File0\"; filename=\""
                            + fileName + "\"" + lineEnd);
                    dos.writeBytes("Content-Type: video/quicktime"
                            + lineEnd + lineEnd);
                } else if (fileName.endsWith(".wav")) {
                    dos.writeBytes("Content-Disposition: form-data; name=\"File0\"; filename=\""
                            + fileName + "\"" + lineEnd);
                    dos.writeBytes("Content-Type: audio/wav" + lineEnd
                            + lineEnd);
                }
                buffer = new byte[bufferSize];
                int bytesRead = 0;
                long count = 0;
                while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                    dos.write(buffer, 0, bytesRead);
                    count = count + bufferSize;
                    publishProgress((int) ((count * 100) / fileSize));
                }
                dos.writeBytes(lineEnd + twoHyphens + boundary + twoHyphens
                        + lineEnd);
                fileInputStream.close();
                dos.flush();
                dos.close();

            } catch (Exception e) {
                e.printStackTrace();
                LogService.appendLog(Log.getStackTraceString(e));
            }
            try {

                inStream = new DataInputStream(conn.getInputStream());
                @SuppressWarnings("unused")
                String str;
                while ((str = inStream.readLine()) != null) {
                }
                inStream.close();
                conn.disconnect();
                return 0;
            } catch (Exception ioex) {
                LogService.appendLog(Log.getStackTraceString(ioex));
                return 2;
            }
        } catch (Exception e) {
            LogService.appendLog(Log.getStackTraceString(e));
            return 2;
        } catch (OutOfMemoryError e) {
            LogService.appendLog(Log.getStackTraceString(e));
            return 1;
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        dialog.setProgress(values[0]);
    }

    @Override
    protected Boolean doInBackground(File... params) {
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (!ApplicationData.isOffline()) {
            try {
                result = postMediaDataToServer(
                        mCurrentEvidence.getEvidenceIdentifier(), params[0]);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (isOfflineEvidence) {
            List<OfflineEvidence> offlineEvidences;

            if (ApplicationData.isAssessor()) {
                offlineEvidences = LoginActivity.assessorOfflineEvidenceDataSource
                        .getAllOfflineEvidences(
                                ApplicationData.getCandidateName(),
                                ApplicationData.getApiPortfolioId());
            } else {
                offlineEvidences = offlineDataSource
                        .getAllOfflineEvidences(ApplicationData
                                .getApiPortfolioId());
            }

            if (evidenceDate != null) {
                for (OfflineEvidence offlineEvidence : offlineEvidences) {
                    if (offlineEvidence.getDate().equals(evidenceDate)) {
                        service.storeMediaToDataBase(offlineEvidence,
                                params[0]);
                    }
                }
            }
        }
        return true;
    }

}

0 个答案:

没有答案