Issue
my android project is having problems with Jsoup. I have a method that gets the data and then puts that data in the gui. Problem i have had is it doesnt wait for the data to be retrieved. I put it in a AsyncTask with the background method using the get data method and on post method using that data. The return on the background method is a arraylist of objects that have the information from the jsoup method.
I can't add the code i am working on but i have added a similar code to a example project. With the same problem.
Code Example Main Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.exampleTextview);
ArrayList<ExampleObject> arrayList = new ArrayList<>();
JsoupClass jsoupClass = new JsoupClass();
arrayList = jsoupClass.getStaffinfomation(arrayList, "examplename");
textView.setText(arrayList.get(0).getName());
}
}
Jsoup Class Example:
public class JsoupClass {
public ArrayList<ExampleObject> getStaffinfomation(final ArrayList<ExampleObject> emptyArray, final String infoFind){
class getStaff extends AsyncTask<ArrayList<ExampleObject>, Void , ArrayList<ExampleObject>> {
@Override
protected ArrayList<ExampleObject> doInBackground(ArrayList<ExampleObject>[] arrayLists) {
Document doc = Jsoup.connect(Config.ExampleURL).get();
}
Elements tableRows = doc.select("tr");
for(int i = 0; i < tableRows.size(); i++) {
if (tableRows.get(i).text().contains(infoFind)) {
//Store the information as a object
String fullname = tableRows.get(i).select("td").get(0).select("a").text();
emptyArray.add(new ExampleObject(fullname));
}
}
}
getStaff getStaff = new getStaff();
getStaff.execute();
return null;
}
}
Object Class
public class ExampleObject {
private String name;
public ExampleObject(String name){
this.name = name;
}
public String getName(){
return name;
}
}
Sorry for not adding the code before, hope this makes sense :)
Solution
Your poblem
Since AsyncTask#doInBackground
run in another thread rather than main thread, your result will not return immediately.It's very normal because main thread should not be blocked. Simply, do long-time task in another thread and update UI in main thread.
How to solve it
/**
* <p>Runs on the UI thread after {@link #doInBackground}. The
* specified result is the value returned by {@link #doInBackground}.</p>
*
* <p>This method won't be invoked if the task was cancelled.</p>
*
* @param result The result of the operation computed by {@link #doInBackground}.
*
* @see #onPreExecute
* @see #doInBackground
* @see #onCancelled(Object)
*/
@SuppressWarnings({"UnusedDeclaration"})
@MainThread
protected void onPostExecute(Result result) {
}
As you see, you can implement onPostExecute
method. The param Result result
is what you return from doInBackground
. Anything confused you can ask me.
Answered By - CoXier
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.