下面是基本上运行的代码......主要是。它适用于我正在测试的所有手机,但在客户端手机上失败,我无法使用。此代码的哪一部分可能导致 NetworkOnMainThreadException?
这真的很简单。我在 onPostExecute() 中所做的是:
- 从 onBackground() 获取 InputStream
- 如果 InputStream 为空则中止,如果为空则中止。
- 检查外部存储可用性,如果没有则中止。
- 从 InputStream 创建文件
- 检查 PDF 查看器的可用性,如果不可用则中止
打开新创建的 PDF
@Override protected void onPostExecute(InputStream is) { super.onPostExecute(is); if (progressDialog.isShowing()) { progressDialog.dismiss(); } if (is== null) { errorMessage = getString(R.string.error_server_communication); CustomDialogs.showErrorDialog( fragment.getActivity(), getString( R.string.error_title ), errorMessage ); return; } //checking external storage avalibility if (!Tools.isWriteableExternalStorageAvalible()) { errorMessage = getString(R.string.error_no_external_storage); CustomDialogs.showErrorDialog( fragment.getActivity(), getString( R.string.error_title ), errorMessage ); return; } // opening pdf try { File directory = new File(Environment.getExternalStorageDirectory(), "MyApp"); if (!directory.exists()) directory.mkdirs(); File file = new File(directory, "file.pdf"); //usuwa poprzednio przechowywany wynik jesli taki byl if (file.exists()) file.delete(); file.createNewFile(); // write the inputStream to a FileOutputStream OutputStream out = new FileOutputStream(file); out.write(IOUtils.toByteArray(is)); is.close(); out.flush(); out.close(); if (Tools.canDisplayPdf(fragment.getActivity())) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/pdf"); startActivity( intent ); } else { //user nie ma przegladarki pdf - komunikat errorMessage = getString(R.string.error_server_communication); CustomDialogs.showErrorDialog( fragment.getActivity(), getString( R.string.error_title ), errorMessage ); } } catch (IOException e) { errorMessage = getString(R.string.error_server_communication); CustomDialogs.showErrorDialog( fragment.getActivity(), getString( R.string.error_title ), errorMessage ); } }
还包括堆栈跟踪。还有一个线索。客户端正在使用 SSL 检索 InputStream,而我没有。不幸的是,还不能用 SSL 调试它。
12-05 21:34:02.648: E/AndroidRuntime(711): FATAL EXCEPTION: main
12-05 21:34:02.648: E/AndroidRuntime(711): android.os.NetworkOnMainThreadException
12-05 21:34:02.648: E/AndroidRuntime(711): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-05 21:34:02.648: E/AndroidRuntime(711): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:668)
12-05 21:34:02.648: E/AndroidRuntime(711): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
12-05 21:34:02.648: E/AndroidRuntime(711): at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
12-05 21:34:02.648: E/AndroidRuntime(711): at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:174)
12-05 21:34:02.648: E/AndroidRuntime(711): at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:188)
12-05 21:34:02.648: E/AndroidRuntime(711): at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:178)
12-05 21:34:02.648: E/AndroidRuntime(711): at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1383)
12-05 21:34:02.648: E/AndroidRuntime(711): at org.apache.commons.io.IOUtils.copy(IOUtils.java:1357)
12-05 21:34:02.648: E/AndroidRuntime(711): at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:361)
12-05 21:34:02.648: E/AndroidRuntime(711): at pl.luxmed.pp.activities.MedicalExaminationsActivity$OpenPdfAsyncTask.onPostExecute(MedicalExaminationsActivity.java:270)
12-05 21:34:02.648: E/AndroidRuntime(711): at pl.luxmed.pp.activities.MedicalExaminationsActivity$OpenPdfAsyncTask.onPostExecute(MedicalExaminationsActivity.java:1)
12-05 21:34:02.648: E/AndroidRuntime(711): at android.os.AsyncTask.finish(AsyncTask.java:631)
12-05 21:34:02.648: E/AndroidRuntime(711): at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-05 21:34:02.648: E/AndroidRuntime(711): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-05 21:34:02.648: E/AndroidRuntime(711): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 21:34:02.648: E/AndroidRuntime(711): at android.os.Looper.loop(Looper.java:137)
12-05 21:34:02.648: E/AndroidRuntime(711): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-05 21:34:02.648: E/AndroidRuntime(711): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 21:34:02.648: E/AndroidRuntime(711): at java.lang.reflect.Method.invoke(Method.java:511)
12-05 21:34:02.648: E/AndroidRuntime(711): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-05 21:34:02.648: E/AndroidRuntime(711): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-05 21:34:02.648: E/AndroidRuntime(711): at dalvik.system.NativeStart.main(Native Method)