Android AsyncTask 仅在实际设备上获得 JSON 而不是 returning/crashing

Android AsyncTask getting JSON but not returning/crashing only on actual device

我有一个 AsyncTask,returns 一个 JSON Object,其中包含一个用户可以连接的潜在服务器列表。如果我使用的是模拟设备,它 returns 没问题,但是,现在我正在实际的平板电脑上测试它,它会抛出错误。令人困惑的是,在我认为是我的 url connection; 的地方抛出了一个空指针错误。但是,AsyncTask 能够记录应在任务结束时返回的适当答案。除此之外,该错误仅在平板电脑上引发 - 它在我使用的所有模拟设备上都运行良好。

AsyncTask(doInBackground):(错误说在包含new InputStreamReader(connectionrates.getInputStream(), "utf-8"));

的行有一个空指针
private class UpdateUtilities extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {

        Log.d("agasg", "asgadsgdsb");
        try {
            byte[] userpass = "something".getBytes(); // can avoid if allow rest call to be done outside of logging in
            URL urlrates = new URL("something");
            String basicAuthrates = "Basic " + Base64.encodeToString(userpass, Base64.DEFAULT);
            HttpURLConnection connectionrates = (HttpURLConnection) urlrates.openConnection();
            connectionrates.setRequestMethod("GET");
            connectionrates.setRequestProperty("Authorization", basicAuthrates);
            BufferedReader inrates = new BufferedReader(
                    new InputStreamReader(connectionrates.getInputStream(), "utf-8"));
            String inputLine;
            StringBuffer responseutils = new StringBuffer();

            while ((inputLine = inrates.readLine()) != null) {
                responseutils.append(inputLine);
            }

            inrates.close();
            Log.d("hi",responseutils.toString());
            return responseutils.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error failed - UtilitiesUpdateDialog";
    }

堆栈跟踪:

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
D/agasg: asgadsgdsb
D/PhoneWindow: *FMB* installDecor mIsFloating : true
D/PhoneWindow: *FMB* installDecor flags : 8388610
D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
I/System.out: (HTTPLog)-Static: (This is just Trace Log)java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
I/System.out:     at com.android.okhttp.internal.http.SBServiceAPI.getService(SBServiceAPI.java:641)
I/System.out:     at com.android.okhttp.internal.http.HttpEngine.<init>(HttpEngine.java:211)
I/System.out:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.newHttpEngine(HttpURLConnectionImpl.java:332)
I/System.out:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.initHttpEngine(HttpURLConnectionImpl.java:283)
I/System.out:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:342)
I/System.out:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:203)
I/System.out:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
I/System.out:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
I/System.out:     at com.example.schadtj.portalapp.Dialogs.UpdateUtilitiesDialogFragment$UpdateUtilities.doInBackground(UpdateUtilitiesDialogFragment.java:119)
I/System.out:     at com.example.schadtj.portalapp.Dialogs.UpdateUtilitiesDialogFragment$UpdateUtilities.doInBackground(UpdateUtilitiesDialogFragment.java:105)
I/System.out:     at android.os.AsyncTask.call(AsyncTask.java:288)
I/System.out:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
I/System.out:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
I/System.out:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
I/System.out:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
I/System.out:     at java.lang.Thread.run(Thread.java:818)
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isShipBuild true
I/System.out: (HTTPLog)-Thread-1399-644722104: SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
**Log is returned here - didn't include result**

您在使用前尚未打开连接。试试这个,让我知道。

connectionrates.connect(); 在做之前 connectionrates.getInputStream()

这肯定会解决这个问题。