Issue
So I'm building a app which is taking some HTML code from a site and returning some data back, but the problem is that AsyncTask which I'm using onCreate doesn't download the whole HTML code just part of it like it crashes at a certain point I searched quite a bit and tried a lot of solutions but none seems to work.
The only "errors" I'm getting is that there is too much work on the main thread, but how I understand AsyncTask is supposed to be done in the background in another thread.
Here is the AsyncTask class:
public class SiteInfo extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
URL url;
String result;
InputStream inputStream;
BufferedReader bufferedReader;
try {
url = new URL(urls[0]);
bufferedReader = new BufferedReader(new InputStreamReader(inputStream = url.openStream()));
String inputLine;
final StringBuffer buffer = new StringBuffer();
while ((inputLine = bufferedReader.readLine()) != null) {
buffer.append(inputLine);
}
bufferedReader.close();
result = buffer.toString();
Log.i("download", result);
inputStream.close();
return result;
}
catch (MalformedURLException e)
{
e.printStackTrace();
return "";
}
catch (IOException e)
{
e.printStackTrace();
return "";
}
catch (Exception e)
{
e.printStackTrace();
return "";
}
}
}
Here is the method which clears the HTML code for the info:
public void getInfo()
{
final SiteInfo task= new SiteInfo();
String result;
try
{
result= task.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR,"*random site url*").get();
String[] temp;
Pattern urlName= Pattern.compile("<div class=\"image\"><img src=\"\"(,*?)\"/");
Matcher matcher=urlName.matcher(result);
for(int i=0; matcher.find(); i++)
{
temp=matcher.group(1).split("\" alt=\"");
imageURL.set(i,temp[i]);
name.set(i,temp[i+1]);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
And last the call of the method in the onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=findViewById(R.id.imageView);
startButton=findViewById(R.id.startButton);
getInfo();
}
Solution
Don't use the AsyncTask.get()
instead you should use new SiteInfo().excute("*random site url*")
to start the AsyncTask and then inside SiteInfo
overraide the onPostExecute()
to get your results.
you may want to make another interface to take back results to your activity.
if you have any questions about how to do it let me know
I checked this now and it's working on my phone, don't use Log to see the html response Log can't view long messages, instead, you can you this Logger
this was my code
public class MainActivity extends AppCompatActivity implements DataInterface
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new SiteInfo(this).execute("https://code.visualstudio.com/");
Logger.addLogAdapter(new AndroidLogAdapter());
}
@Override
public void returnData(String result) {
Logger.e(result);
}
public class SiteInfo extends AsyncTask<String, Void, String> {
DataInterface dataInterface;
SiteInfo(DataInterface dataInterface) {
super();
this.dataInterface = dataInterface;
}
@Override
protected String doInBackground(String... urls) {
URL url;
String result;
InputStream inputStream;
BufferedReader bufferedReader;
try {
url = new URL(urls[0]);
bufferedReader = new BufferedReader(new InputStreamReader(inputStream = url.openStream()));
String inputLine;
final StringBuilder buffer = new StringBuilder();
while ((inputLine = bufferedReader.readLine()) != null) {
buffer.append(inputLine);
}
bufferedReader.close();
result = buffer.toString();
Log.i("download", result);
inputStream.close();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
@Override
protected void onPostExecute(String result) {
dataInterface.returnData(result);
}
}
}
and the interface is
public interface DataInterface {
void returnData(String result);
}
Answered By - Ezaldeen sahb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.