我是Java编程的新手,有一个问题。我发现this帖子,并尝试使用此方法将我的值从doInBackground方法传递给onPostExecute方法。但是Android Studio 2.3不允许这样做。

Android Studio在onPostExecute上强调了我的@Override:

@Override
    protected void onPostExecute(String s){
        //super.onPreExecute();

        // Create adapter for ListView (Universal Image Loader)
        AngeboteListAdapter adapter = new AngeboteListAdapter(this, R.layout.angebote_list_view_adapter, dataList);
        mListView.setAdapter(adapter);

       Toast.makeText(AngeboteActivity.this,"onPostExecute",Toast.LENGTH_LONG).show();
    }


和我的QueryResult在doInBackground中:

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


这是所有代码:

public class MultiplyTask extends AsyncTask<String,Void,ArrayList<Artikel>>{

    private class QueryResult {
        ArrayList<Artikel> dataList;

        public QueryResult(ArrayList<Artikel> dataList) {
            this.dataList = dataList ;
        }
    }

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
    }
    @Override
    protected void onPostExecute(String s){
        //super.onPreExecute();

        // Create Adapter for ListView (UniversalImageLoader)
        AngeboteListAdapter adapter = new AngeboteListAdapter(this, R.layout.angebote_list_view_adapter, dataList);
        mListView.setAdapter(adapter);

        Toast.makeText(AngeboteActivity.this,"onPostExecute",Toast.LENGTH_LONG).show();
    }


    String data ="";

    @Override
    protected QueryResult doInBackground(String... params){
        try
        {
         URL url = new URL("https://myjson.com");
         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;
         }
         ArrayList<String> listdata = new ArrayList<>();
         JSONArray jArray = new JSONArray(data);
         for(int i =0 ;i <jArray.length(); i++){
             listdata.add(jArray.getString(i));
        }

            JSONArray json = new JSONArray(data);
            String[][] matrix = new String[json.length()][5];


         //Fill Array with response
        for (int i=0; i < json.length(); i++) {
            JSONObject obj = json.getJSONObject(i);
            matrix[i][0] = String.valueOf(obj.getInt("Artikelnummer"));
            matrix[i][1] = String.valueOf(obj.getDouble("Preis"));
            matrix[i][2] = obj.getString("Von");
            matrix[i][3] = obj.getString("Bis");
            matrix[i][4] = obj.getString("art_link");
        }



            //new Arrays
            String[] all_art_nr = new String[matrix.length];
            String[] all_preis = new String[matrix.length];
            String[] all_von = new String[matrix.length];
            String[] all_bis = new String[matrix.length];
            String[] all_link = new String[matrix.length];


            //Array sort
            for (int i = 0; i < matrix.length; i++) {
                all_art_nr[i] = matrix[i][0];
                all_preis[i] = matrix[i][1];
                all_von[i] = matrix[i][2];
                all_bis[i] = matrix[i][3];
                all_link[i] = matrix[i][4];
            }



            //Fill Arraylist
            ArrayList<Artikel> dataList = new ArrayList<>();
            for (int i = 0; i < matrix.length; i++) {
                Artikel angebote = new Artikel(all_art_nr[i], all_preis[i], all_von[i], all_bis[i], all_link[i]);
                dataList.add(angebote);
            }


            return new QueryResult(dataList);


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

    return null;
    }

}


Android Studio想要将QueryResult更改为Arraylist,并且@Override不会从其超类重写方法,如果我在doINBackground中将QueryResult移出,则他可以重写超类方法。提前致谢。

最佳答案

扩展Asynctask时,请检查参数:
extends Asynctask<Params, Progress, Result>

因此,如果要将字符串传递给doInBackround,这会将QueryResult返回到onPostExecute,则应该这样扩展:
extends Asynctask<String, Void, QueryResult>

09-05 17:48