I'm new to Java and I can't seem to wrap my head around this one.
I am trying to fetch the contents of a web page (txt file) from a URL and add each line to an ArrayList.
I can spit the array out using Log.d in onPostExecute
, but I would like to be able to return the ArrayList in MainActivity to then do some validation and pass the results onto another class for processing.
I have tried to follow this answer: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
I'm getting the following error:
Error:(25, 10) error: processFinish(ArrayList) in MainActivity cannot implement processFinish(ArrayList) in AsyncResponse attempting to assign weaker access privileges; was public
Any help would be greatly appreciated.
MainActivity:
package uk.co.mmotti.nothanks;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import uk.co.mmotti.nothanks.HostProcessing.fetchHosts;
public class MainActivity extends AppCompatActivity implements AsyncResponse {
fetchHosts asyncFetchHosts = new fetchHosts();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
asyncFetchHosts.delegate = this;
asyncFetchHosts.execute("https://exampletexturl.com/");
}
@Override
/* THIS IS WHERE THE ERROR OCCURS ************************/
void processFinish(ArrayList<String> output){
//Here you will receive the result fired from async class
//of onPostExecute(result) method.
}
}
fetchHosts
package uk.co.mmotti.nothanks.HostProcessing;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import uk.co.mmotti.nothanks.AsyncResponse;
public class fetchHosts extends AsyncTask<String, Integer, ArrayList<String>> {
public AsyncResponse delegate = null;
@Override
protected ArrayList<String> doInBackground(String...hostUrl) {
ArrayList<String> hosts = new ArrayList<>();
try
{
// Set the URL to the host parameter
URL url = new URL(hostUrl[0]);
// Open a new stream to the url
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
// Clear any values that may already be in 'line'
String readerLine = null;
// Whilst there are still lines to output
while((readerLine = in.readLine()) != null)
{
// Add host to array
hosts.add(readerLine);
}
// Close stream when done
in.close();
}
// On error exception
catch (IOException e)
{
e.printStackTrace();
}
// Output to PostExecute
return hosts;
}
protected void onPreExecute() {
}
protected void onProgressUpdate(Integer... values) {
}
@Override
protected void onPostExecute(ArrayList<String> hosts) {
//Log.d("MM: ", hosts.toString());
delegate.processFinish(hosts);
}
}
AsyncResponse (Separate class / interface)
package uk.co.mmotti.nothanks;
import java.util.ArrayList;
public interface AsyncResponse {
void processFinish(ArrayList<String> output);
}