AsyncTask:无法解决构造函数错误

时间:2019-11-10 14:06:43

标签: java android android-intent

我想将一个整数传递给另一个类。在Intent intent = new Intent(this,favaoritesFragment.class)部分,发生构造函数错误。我尝试将this更改为getActivityfetchdata.this,但它们都不起作用。有人可以给我一个建议来解决它。

package com.morimoku.location_activity;

import android.content.Intent;
import android.os.AsyncTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
            public class fetchdata extends AsyncTask<Void,Void,Void> {
        String data = "";
        String dataParsed = "";
        String singleParsed = "";
         public int i1;
        @Override
        protected Void doInBackground(Void... voids) {
            try {
                URL url = new URL("URL");
                HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String line ="";
                while (line!=null){
                    line = bufferedReader.readLine();
                    data = data+line;
                }
                JSONArray JA = new JSONArray(data);
                for(int i=0;i<JA.length();i++){
                    JSONObject JO= (JSONObject)JA.get(i);
                    singleParsed = (String) JO.get("keihinTohokuNegishi")+JO.get("trainInormationText");
                    if ("trainInformationText"=="normal"){
                        i1 = 0;
                    }else{
                        i1 = 1;
                    }

                    dataParsed = dataParsed + singleParsed;
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }
    @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                Intent intent = new Intent(this,favaoritesFragment.class)
         }
        }

1 个答案:

答案 0 :(得分:0)

首先,请注意AsyncTask在Android 11中将被标记为已弃用。AsyncTask已经过时了几年。我建议您专注于使用最新资料学习Android,而其中的任何内容都不推荐AsyncTask

话虽如此,您将需要在Context类中添加一个fetchdata字段,可能是通过构造函数参数填充的。然后,您可以在Context的{​​{1}}构造函数中使用该Intent