API 26在后台对HttpsURLConnection的问题

时间:2019-03-24 08:32:18

标签: android

谁会告诉您为什么当您在打开的应用程序中使用HttpURLConnection启动服务时,一切运行正常,并且当您拧紧该应用程序时,urlConnection.connect()会在此处停止,并在再次启动时开始工作另一刻,检查了服务中的Internet,有一个开放的激活,但是在封闭的状态中,由于某种原因没有激活,我无法弄清楚整个Internet,我能理解清单中的某些内容吗? / p>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity"
                android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:enabled="true" android:name=".LoadDataService"/>
</application>

执行服务

    public class TestActivity extends Fragment {
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_monitor, container, false);

        final Button start_monitoring = rootView.findViewById(R.id.start_monitoring);

        final Context context = rootView.getContext().getApplicationContext();
        View.OnClickListener start_monitoring_lsnr = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             Intent intent = new Intent(context, LoadDataService.class);
              context.startService(intent);
            }

        };
return rootView;
     }
}

服务

    public class LoadDataService extends Service
{
    private DatabaseHandler db;
    private static Context cur_context;
    ExecutorService es;

    @Override
    public void onCreate() {
        super.onCreate();
        this.cur_context = getBaseContext();
        db = new DatabaseHandler(cur_context);
        es = Executors.newFixedThreadPool(1);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        StartService ss = new StartService(intent);
        es.execute(ss);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    class StartService implements Runnable{

        Intent intent;

        public StartService(Intent intent) {
            this.intent = intent;
        }

        @Override
        public void run() {

           try {               
                HttpsURLConnection connection = null;
                connection = (HttpsURLConnection) new URL(url).openConnection();
                connection.setRequestMethod("GET");
                connection.setHostnameVerifier(hostnameVerifier);
                connection.connect();
                InputStream inputStream = connection.getInputStream();
  

InputStream inputStream = connection.getInputStream(); 在此处,当应用程序处于非活动状态时,代码将停止,并且当应用程序处于活动状态时,一切都会正常运行

            } catch (Exception ex) {
                ex.printStackTrace();
            }
    }
 }
}

Logcat

2019-04-02 20:16:50.698 8888-9803/? E/CrashlyticsCore: Error occurred sending report com.crashlytics.android.core.SessionReport@6cab92f
io.fabric.sdk.android.services.network.HttpRequest$HttpRequestException: java.net.SocketTimeoutException: failed to connect to reports.crashlytics.com/54.221.227.86 (port 443) from /10.119.96.97 (port 54463) after 10000ms
    at io.fabric.sdk.android.services.network.HttpRequest.part(HttpRequest.java:2550)
    at io.fabric.sdk.android.services.network.HttpRequest.part(HttpRequest.java:2530)
    at io.fabric.sdk.android.services.network.HttpRequest.part(HttpRequest.java:2516)
    at com.crashlytics.android.core.DefaultCreateReportSpiCall.applyMultipartDataTo(DefaultCreateReportSpiCall.java:89)
    at com.crashlytics.android.core.DefaultCreateReportSpiCall.invoke(DefaultCreateReportSpiCall.java:61)
    at com.crashlytics.android.core.CompositeCreateReportSpiCall.invoke(CompositeCreateReportSpiCall.java:18)
    at com.crashlytics.android.core.ReportUploader.forceUpload(ReportUploader.java:104)
    at com.crashlytics.android.core.ReportUploader$Worker.attemptUploadWithRetry(ReportUploader.java:242)
    at com.crashlytics.android.core.ReportUploader$Worker.onRun(ReportUploader.java:185)
    at io.fabric.sdk.android.services.common.BackgroundPriorityRunnable.run(BackgroundPriorityRunnable.java:30)
    at java.lang.Thread.run(Thread.java:764)
 Caused by: java.net.SocketTimeoutException: failed to connect to reports.crashlytics.com/54.221.227.86 (port 443) from /10.119.96.97 (port 54463) after 10000ms
    at libcore.io.IoBridge.connectErrno(IoBridge.java:185)
    at libcore.io.IoBridge.connect(IoBridge.java:130)
    at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:129)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:356)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
    at java.net.Socket.connect(Socket.java:616)
    at com.android.okhttp.internal.Platform.connectSocket(Platform.java:145)
    at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:141)
    at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
    at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
    at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
    at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:258)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(Unknown Source:0)
    at io.fabric.sdk.android.services.network.HttpRequest.openOutput(HttpRequest.java:2454)
    at io.fabric.sdk.android.services.network.HttpRequest.startPart(HttpRequest.java:2467)
    at io.fabric.sdk.android.services.network.HttpRequest.part(HttpRequest.java:2546)
    at io.fabric.sdk.android.services.network.HttpRequest.part(HttpRequest.java:2530) 
    at io.fabric.sdk.android.services.network.HttpRequest.part(HttpRequest.java:2516) 
    at com.crashlytics.android.core.DefaultCreateReportSpiCall.applyMultipartDataTo(DefaultCreateReportSpiCall.java:89) 
    at com.crashlytics.android.core.DefaultCreateReportSpiCall.invoke(DefaultCreateReportSpiCall.java:61) 
    at com.crashlytics.android.core.CompositeCreateReportSpiCall.invoke(CompositeCreateReportSpiCall.java:18) 
    at com.crashlytics.android.core.ReportUploader.forceUpload(ReportUploader.java:104) 
    at com.crashlytics.android.core.ReportUploader$Worker.attemptUploadWithRetry(ReportUploader.java:242) 
    at com.crashlytics.android.core.ReportUploader$Worker.onRun(ReportUploader.java:185) 
    at io.fabric.sdk.android.services.common.BackgroundPriorityRunnable.run(BackgroundPriorityRunnable.java:30) 
    at java.lang.Thread.run(Thread.java:764) 

我试图在本主题中同时做这两项-this problem,但仍然没有帮助

请帮助!

0 个答案:

没有答案