我是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();
}
和doInBackground中的QueryResult:
@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,他可以覆盖超类方法。提前谢谢。
答案 0 :(得分:1)
当扩展Asynctask 时,请检查参数:
extends Asynctask<Params, Progress, Result>
因此,如果您想将字符串传递给 doInBackround ,这会将 QueryResult 返回到 onPostExecute 你应该像这样扩展:
extends Asynctask<String, Void, QueryResult>
答案 1 :(得分:0)
为什么这不起作用?
private class QueryResult {
ArrayList<Artikel> dataList;
public QueryResult(ArrayList<Artikel> dataList) {
this.dataList = dataList ;
}
}
public class loadAngebote extends AsyncTask<String, Void, ArrayList<Artikel>>{
String data ="";
@Override
protected ArrayList<Artikel> doInBackground(String... params){
try
{
URL url = new URL("https://api.myjson.com/bins/1dnrmf");
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];
//Array mit Response füllen
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");
}
//Neue Arrays inizializieren
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 umsotieren und befüllen
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];
}
//String in Arraylist vom Typ Artikel einfügen
ArrayList<Artikel> dataList = new ArrayList<>();
//ArrayList<Artikel> passed = passing[0]; //get passed 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;
}
@Override
protected void onPostExecute(ArrayList<Artikel> QueryResult){
//super.onPreExecute();
TextView txt = (TextView) findViewById(R.id.data);
txt.setText("Executed"); // txt.setText(result);
// Adapter erstellen und dem ListView übergeben
AngeboteListAdapter adapter = new AngeboteListAdapter(this, R.layout.angebote_list_view_adapter, dataList);
mListView.setAdapter(adapter);
Toast.makeText(AngeboteActivity.this,"onPostExecute Methode ausgeführt",Toast.LENGTH_LONG).show();
}
}