簡體   English   中英

我無法從 AsyncTask 子類中的 doInBackground 方法中獲取數據

[英]I can't fetch data from the doInBackground method in AsyncTask Subclass

我正在嘗試在我的應用程序中獲取地震數據並讓我的應用程序停止工作。 我的代碼沒有錯誤。 甚至我添加了互聯網許可。 但是在獲取數據時,我認為會發生此錯誤。 我已經調試過,但找不到問題。

日志貓:

2021-03-27 15:23:32.118 9533-9560/com.example.quakereportapp E/eglCodecCommon:glUtilsParamSize:未知參數 0x000082da 2021-03-27 15:23:32.118 9533-9560/com.example.quakereportapp E/eglCodec :glUtilsParamSize:未知參數 0x000082da ioctl_ping 對於 device_type=5 失敗,ret=-1 2021-03-27 15:23:37.091 9533-9533/com.example.quakereportapp E/AndroidRuntime:致命異常:主進程:Z4D236D9A2D102C5FE6ADC1。 quakereportapp, PID: 9533 android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513) at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:415) at com.android .org.conscrypt.ConscryptFileDescriptorSocket.shutdownAndFreeSslNative(ConscryptFileDescriptor Socket.java:1005) at com.android.org.conscrypt.ConscryptFileDescriptorSocket.close(ConscryptFileDescriptorSocket.java:1000) at com.android.okhttp.internal.Util.closeQuietly(Util.java:86) at com.android.okhttp .internal.http.StreamAllocation.deallocate(StreamAllocation.java:256) at com.android.okhttp.internal.http.StreamAllocation.connectionFailed(StreamAllocation.java:293) at com.android.okhttp.internal.http.HttpEngine.close (HttpEngine.Z93F725A07423FE1C889F448B33D21F 46Z:445) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:509) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407) at com.android .okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538) at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105) at com.android.okhttp.internal.huc.HttpsURLConnectionImpl .getResponseCode(HttpsURLConnectionImpl.java:26) 在 Z4D236D9A2D102C5FE6A D1C50DA4BEC50Z.example.quakereportapp.QueryUtils.makeHttpRequest(QueryUtils.java:82) at com.example.quakereportapp.QueryUtils.fetchEarthquakeData(QueryUtils.java:35) at com.example.quakereportapp.MainActivity.updateUi(MainActivity.java:27) at com.example.quakereportapp.MainActivity.access$100(MainActivity.java:14) at com.example.quakereportapp.MainActivity$EarthQuakeAsyncTask.onPostExecute(MainActivity.java:58) at com.example.quakereportapp.MainActivity$EarthQuakeAsyncTask.onPostExecute( MainActivity.java:33) at android.os.AsyncTask.finish(AsyncTask.java:695) at android.os.As yncTask.access$600(AsyncTask.java:180) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper. loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller. run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我的 AsyncTask 子類

private void updateUi(List<Earthquake> earthquakes) {
    listView = findViewById(R.id.list_view);
    ArrayList<Earthquake> earthquake = QueryUtils.fetchEarthquakeData(url);
    EarthquakeAdapter earthquakeAdapterArrayAdapter = new EarthquakeAdapter(this, earthquake);
    listView.setAdapter(earthquakeAdapterArrayAdapter);
}
private class EarthQuakeAsyncTask extends AsyncTask<String, Void, List<Earthquake>> {
 
    @Override
    protected  ArrayList<Earthquake> doInBackground(String... urls) {
        ArrayList<Earthquake> result = QueryUtils.fetchEarthquakeData(urls[0]);
        return result;
    }


    @Override
    protected void onPostExecute(List<Earthquake> result) {
        updateUi(result);
    }

我的 JSON 來自 QueryUtils Class 的提取器方法

private static ArrayList<Earthquake> extractFeatureFromJson(String earthquakeJSON) {
    // If the JSON string is empty or null, then return early.
    if (TextUtils.isEmpty(earthquakeJSON)) {
        return null;
    }

    ArrayList<Earthquake> earthquakes = new ArrayList<>();


    try {
        JSONObject baseJsonResponse = new JSONObject(earthquakeJSON);
        JSONArray earthquakeArray = baseJsonResponse.getJSONArray("features");

        for (int i = 0; i < earthquakeArray.length(); i++) {
            //creates object from the given array
            JSONObject currentEarthquake = earthquakeArray.getJSONObject(i);
            //stores all the data from properties to properties object
            JSONObject properties = currentEarthquake.getJSONObject("properties");
            //here we assign the value of the specified keys from the properties
            String magnitude = properties.getString("mag");
            String location = properties.getString("place");
            long time = properties.getLong("time");
            //and now we create a new earthquake in every loop
            Earthquake earthquake = new Earthquake(magnitude, location, time);
            //finally we add it to the earthquakes list
            earthquakes.add(earthquake);
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Problem parsing the earthquake JSON results", e);
    }
    return earthquakes;
}

拋出異常是因為您正在從主線程調用 API。 您的代碼中的問題是您兩次調用 API。 一次在doInBackground中,然后再次在updateUi中。 updateUi中刪除對QueryUtils.fetchEarthquakeData(url)的調用,它應該如下所示。

private void updateUi(List<Earthquake> earthquakes) {
    listView = findViewById(R.id.list_view);
    EarthquakeAdapter earthquakeAdapterArrayAdapter = new EarthquakeAdapter(this, earthquakes);
    listView.setAdapter(earthquakeAdapterArrayAdapter);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM