Android Studio Activity start delayed - java

I got a AsyncTask:
package e.marco.swimcommit;
import android.os.AsyncTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class News extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... strings) {
final StringBuilder builder = new StringBuilder();
final StringBuilder builder2 = new StringBuilder();
{
try {
Document doc = Jsoup.connect("http://www.schwimmclub-schwandorf.de/index.php/8-home/56-infos-neuigkeiten").get();
String title = doc.title();
Elements links = doc.select("h2");
Elements links2 = doc.select("h3");
builder.append(title).append("\n");
for (Element link : links) {
builder.append(link.text()).append("$");
}
for (Element link : links2) {
builder2.append(link.text()).append("$");
}
} catch (IOException e) {
e.printStackTrace();
}
}
String text = builder.toString() + "%" + builder2.toString();
return text;
}
}
and a onResume Methode in my MainActivity which set the returned text in a textview
#Override
protected void onResume()
{
super.onResume();
try {
eins.setText(new News().execute().get());
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
But if i start the App it shows me a white Screen until onResume Methode get the text and set it to the Textview. How is it possible to load the App show all other Elements like Buttons Background and so on without the delayed start? So that the Textview is blank until the onResume Methode get the Information and set it?
Edit: Without blocking the UI

AsyncTask is used to perform background operations and publish results on the UI thread. In your case you should put eins.setText into onPostExecute of AsyncTask.
Another problem is because the AsyncTask is a separate class so you need to define an interface to pass result back to MainActivity.
News
public class News extends AsyncTask<String, Void, String> {
private WeakReference<OnNewsListener> mOnNewsListener;
public void setOnNewsListener(OnNewsListener listener) {
mOnNewsListener = new WeakReference<>(listener);
}
#Override
protected String doInBackground(String... strings) {
final StringBuilder builder = new StringBuilder();
final StringBuilder builder2 = new StringBuilder();
{
try {
Document doc = Jsoup.connect("http://www.schwimmclub-schwandorf.de/index.php/8-home/56-infos-neuigkeiten").get();
String title = doc.title();
Elements links = doc.select("h2");
Elements links2 = doc.select("h3");
builder.append(title).append("\n");
for (Element link : links) {
builder.append(link.text()).append("$");
}
for (Element link : links2) {
builder2.append(link.text()).append("$");
}
} catch (IOException e) {
e.printStackTrace();
}
}
String text = builder.toString() + "%" + builder2.toString();
return text;
}
#Override
protected void onPostExecute(String text) {
if (mOnNewsListener != null) {
if (mOnNewsListener.get() != null) {
mOnNewsListener.get().onNews(text);
}
}
}
public interface OnNewsListener {
void onNews(String text);
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements News.OnNewsListener{
TextView eins;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eins = findViewById(R.id.eins);
}
#Override
protected void onResume() {
super.onResume();
News news = new News();
news.setOnNewsListener(this);
news.execute();
}
#Override
public void onNews(String text) {
eins.setText(text);
}
}

As suggested by pz64, set the text in onPostExecute() method and call the AsyncTask() without calling get() method. get() method on AsyncTask makes task synchronous and also affects your UI.
public class News extends AsyncTask<String, Void, String> {
#override
protected void onPreExecute(){
//initiate your loading views
}
#Override
protected String doInBackground(String... strings) {
final StringBuilder builder = new StringBuilder();
final StringBuilder builder2 = new StringBuilder();
{
try {
Document doc = Jsoup.connect("http://www.schwimmclub-schwandorf.de/index.php/8-home/56-infos-neuigkeiten").get();
String title = doc.title();
Elements links = doc.select("h2");
Elements links2 = doc.select("h3");
builder.append(title).append("\n");
for (Element link : links) {
builder.append(link.text()).append("$");
}
for (Element link : links2) {
builder2.append(link.text()).append("$");
}
} catch (IOException e) {
e.printStackTrace();
}
}
String text = builder.toString() + "%" + builder2.toString();
return text;
}
#override
protected void onPostExecute(String response){
//dispose loading views
if(response != null){
eins.setText(response);
}else{
//could not load
}
}
}
Call:
#Override
protected void onResume()
{
super.onResume();
new News().execute(); //do not call get method
}

You can call asyncTask in oncreate method.
And set the result in onProgressUpdate method.
#Override
protected void onProgressUpdate(String... text) {
eins.setText.setText(text);
}

Related

I'm having problem while scraping data from this website. My code is below:

I want to fecth the data(Marked in the picture)from this URL using Jsoup that I used in my code below.
But I'm having error while doing this. I don't get any value in my String description.
In the MainActivity I declared and intialized the class's object,
description_webscrape dw = new description_webscrape();
dw.execute();
Here's my code of another class.
public class description_webscrape extends AsyncTask<Void,Void,Void>
{
String url = "https://www.ebay.com/sch/i.html?_from=R40&_nkw=shoes&_sacat=0&rt=nc&LH_Sold=1&LH_Complete=1";
description_webscrape(){};
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... voids) {
org.jsoup.nodes.Document document = null;
try {
document = Jsoup.connect(url).get();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "doInBackground: errorrr---"+e);
}
org.jsoup.select.Elements elements = document.getElementsByClass("srp-controls__count-
heading");
//description is String type and declared globally
description = elements.text();
return null;
}
#Override
protected void onPostExecute(Void unused) {
textView.setText(description+"");
}
}

Why is my code not working in a AsyncTask using the JSoup library for my Android activity?

public class ConnectionTest extends AsyncTask<Void, Void, Void> {
String connection;
String loginFormUrl = "https://intranet.tam.ch/";
#Override
protected Void doInBackground(Void... voids) {
try{
Connection.Response loginForm = Jsoup.connect(loginFormUrl).method(Connection.Method.GET)
.execute();
connection = loginForm.toString();
System.out.print(title);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
My Activity should just display the connection in a TextView. I have also tried making a Thread and running it in the new Thread but it also won't work.
Here is my Activity
public class Test extends AppCompatActivity {
TextView textView;
ConnectionTest connectionTest = new ConnectionTest();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
connectionTest.getWebsite();
textView = findViewById(R.id.sdweedew);
textView.setText(connectionTest.connection);
}
}
Change your activity code like this,
I've used a TextView to display the status of the connection.
public class MainActivity extends AppCompatActivity {
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
textView = findViewById(R.id.sdweedew);
new ConnectionTest().execute();
}
class ConnectionTest extends AsyncTask<Void, Void, String> {
String loginFormUrl = "https://intranet.tam.ch/";
#Override
protected String doInBackground(Void... voids) {
try {
Connection.Response loginForm = Jsoup.connect(loginFormUrl).method(Connection.Method.GET)
.execute();
return loginForm.statusMessage();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String s) {
if (textView != null) {
textView.setText(s);
}
super.onPostExecute(s);
}
}
}
And remember to add Internet permission in manifest file.
<uses-permission android:name="android.permission.INTERNET" />
please check this code
private void getWebsite() {
new Thread(new Runnable() {
#Override
public void run() {
final StringBuilder builder = new StringBuilder();
try {
Document doc = Jsoup.connect("https://intranet.tam.ch/").get();
String title = doc.title();
Elements links = doc.select("a[href]");
builder.append(title).append("\n");
for (Element link : links) {
builder.append("\n").append("Link : ").append(link.attr("href"))
.append("\n").append("Text : ").append(link.text());
}
} catch (IOException e) {
builder.append("Error : ").append(e.getMessage()).append("\n");
}
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(builder.toString());
}
});
}
}).start();
}

Problem with accessing the views from exception within asyncTask

I am having a problem which is I think that I can't access th,e ListView from the asyncTask
Actually, I really don't know the real problem here
Let me show you what is happening
I have an activity which is executing AsyncTask and creates HttpURLConnection. Sometimes I get an exception (ProtocolException) because the stream un-expectedly ends.
So, I created a handler for this exception that calls a function or a method inside the class of the activity to display a message to the user
Here is a picture so you understand what is my project.
image
the problem here whenever the exception is thrown, the same function/method that I use to add the text to the listView is called, but after it called the listView disappear, but when I minimize the soft keyboard manually the everything becomes fine.
the structure of my class is
public class MainActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
addMessageToListView()//works fin here
}
protected void addMessage(String message, int userMessage, ListView listView) // the function
{
try
{
messages.add(new Message(message,userMessage));
MessagesAdapter messagesAdapter = new MessagesAdapter(messages, getBaseContext());
messagesAdapter.notifyDataSetChanged();
listView.setAdapter(messagesAdapter);
}
catch (Exception exception)
{
}
}
private class HttpPostAsyncTask extends AsyncTask<String, Void, String>
{
...
#Override
protected void onPostExecute(String result)
{
try
{
addMessageToListView()//works fin here
}
catch (Exception exception)
{
}
}
protected String doInBackground(String... params)
{
String result = "";
for (int i = 0; i <= 0; ++i)
{
result = this.invokePost(params[i], this.postData);
}
return result;
}
private String invokePost(String requestURL, HashMap<String, String> postDataParams)// called from doInBackground
{
try
{
addMessageToListView()//works fin here
}
catch (Exception exception)
{
addMessageToListView()//not orking here
}
}
}
}
I don't know how to explain more actually.
You can change Views only in mainthread of your app. The doInBackground doesn't run in mainthread of your app.
Solved by adding:
new Thread()
{
#Override
public void run()
{
MainActivity.this.runOnUiThread(new Runnable()
{
#Override
public void run()
{
addMessageToListView()//not orking here
}
});
super.run();
}
}.start();
Editing the previous code in my question:
public class MainActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
addMessageToListView()//works fin here
}
protected void addMessage(String message, int userMessage, ListView listView) // the function
{
try
{
messages.add(new Message(message,userMessage));
MessagesAdapter messagesAdapter = new MessagesAdapter(messages, getBaseContext());
messagesAdapter.notifyDataSetChanged();
listView.setAdapter(messagesAdapter);
}
catch (Exception exception)
{
}
}
private class HttpPostAsyncTask extends AsyncTask<String, Void, String>
{
...
#Override
protected void onPostExecute(String result)
{
try
{
addMessageToListView()//works fin here
}
catch (Exception exception)
{
}
}
protected String doInBackground(String... params)
{
String result = "";
for (int i = 0; i <= 0; ++i)
{
result = this.invokePost(params[i], this.postData);
}
return result;
}
private String invokePost(String requestURL, HashMap<String, String> postDataParams)// called from doInBackground
{
try
{
addMessageToListView()//works fin here
}
catch (Exception exception)
{
new Thread()
{
#Override
public void run()
{
MainActivity.this.runOnUiThread(new Runnable()
{
#Override
public void run()
{
addMessageToListView()//not orking here
}
});
super.run();
}
}.start();
}
}
}
}

how to return string from AsyncTask to another activity

I have created a class and extend with AsyncTask , now i want to return me a string to another activity throw this class . its getting jsonstring from mysql mDatabase
enter code here
public class GetJson extends AsyncTask<Void,Void,String> {
String JSON_STRING;
String json_url;
public String pasString;
Activity ab;
#Override
protected void onPreExecute() {
json_url="https://XXXXXXX.000webhostapp.com/Json_getData_Survey.php";
}
public GetJson(Activity b) {
ab=b;
}
#Override
protected String doInBackground(Void... params) {
try {
URL url =new URL(json_url);
HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
InputStream inputStream =httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder=new StringBuilder();
while ((JSON_STRING=bufferedReader.readLine())!=null)
{
stringBuilder.append(JSON_STRING+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
String strings =stringBuilder.toString().trim();
return strings;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(String result) {
pasString=result;
// Toast.makeText(ab,pasString,Toast.LENGTH_LONG).show();
// json_string1=result;
}
}
i just need to return string1 to my another Activity , i have creaetd a another mehtod in GetJson class to return this string but when i display this it show nothing in it. please help
its Quite simple
make an interface class as following
public interface ResponseListener() {
void onResponseReceive(String data);
}
and add instance of this class in you GetJson class like this
public class GetJson extends AsyncTask<Void,Void,String> {
String JSON_STRING;
String json_url;
public String pasString;
Activity ab;
// this is new code
ResponseListener listener;
public void setOnResponseListener(ResponseListener listener) {
this.listener = listener;
}
#Override
protected void onPreExecute() {
json_url="https://XXXXXXX.000webhostapp.com/Json_getData_Survey.php";
}
// now add this code in onPost function
#Override
protected void onPostExecute(String result) {
pasString=result;
listener.onResponseReceive(pasString);
// Toast.makeText(ab,pasString,Toast.LENGTH_LONG).show();
// json_string1=result;
}
now when you call GetJson from your activity, just simple do that;
GetJson json = new GetJson(Activity.this);
json.setOnResponseListener(new ResponseListener() {
#Override
public void onResponseReceived(String data) {
// here you will get your response
}
});
hope you will understand. :)
Try using activity results. You can set the return value on the one activity, and get it when that activity terminates.
https://developer.android.com/training/basics/intents/result.html

VISIBLE cannot be resolved to a variable

I'm not sure exactly how this can be corrected but I'm getting an error stating: VISIBLE cannot be resolved to a variable. Any suggestions are greatly appreciated. Thus far I've looked over:
http://developer.android.com/reference/android/view/View.html#setVisibility(int)
but I do not understand exactly how this can be implemented in this case.
SOURCE:
public class MainActivity extends Activity {
private TextView textView;
private String response;
public interface Callback {
void onModifiedTextView(String value);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.TextView01);
textView.setVisibility(VISIBLE);
}
public void onModifiedTextView(final String title) {
runOnUiThread(new Runnable() {
public void run() {
textView.setText(title);
textView.invalidate(); // not even necessary
}
});
}
public class DownloadWebPageTask extends AsyncTask<String, Void, String> {
public DownloadWebPageTask(MainActivity mainActivity) {
this.callback = mainActivity;
}
private MainActivity callback;
private String title;
public DownloadWebPageTask() {
// TODO Auto-generated constructor stub
}
public DownloadWebPageTask(TextView textView) {
// TODO Auto-generated constructor stub
}
#Override
protected String doInBackground(String... urls) {
String response = title;
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
Document doc = Jsoup.connect("http://google.com")
.userAgent("Mozilla")
.get();
// get page title
String title = doc.title();
System.out.println("title : " + title);
// get all links
Elements links = doc.select("a[href]");
for (Element link : links) {
// get the value from href attribute
System.out.println("\nlink : " + link.attr("href"));
System.out.println("text : " + link.text());
}
} catch (IOException e) {
e.printStackTrace();
}
}
// callback.onModifiedTextView(response);
return response;
}
#Override
protected void onPostExecute(final String title) {
callback.onModifiedTextView(title);
callback.onModifiedTextView(response);
}
}
public void onClick(View view) {
DownloadWebPageTask task = new DownloadWebPageTask(this);
task.execute(new String[] { "http://www.google.com" });
}
}
VISIBLE is an integer value in the View class. You should change this to View.VISIBLE instead of just VISIBLE, unless you are inside a custom view.
In addtion to #kcoppock answer. You can use
you can use this import:
import static android.view.View.VISIBLE;
or just
textView.setVisibility(View.VISIBLE);
VISIBLE is defined as static final int in the View class.

Categories