I want to fetch data from two tables, Employee and Education, in odata4j. The common column is EmployeeId.
How can I join the tables? I use this for fetching data but it takes more time then expected.
private void DataListPosition() {
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... voids) {
try {
ConnectionOData4j connectionClass = new ConnectionOData4j(context);
ODataConsumer consumer = connectionClass.getConnection();
for(OEntity entity : consumer.getEntities("Employee").execute()){
final String name=entity.getProperty("Name" , String.class).getValue();
((Activity) context).runOnUiThread(new Runnable() {
public void run() {
EmployeeName=name;
}
});
}
}catch (Exception e) {
final String strError=e.getMessage();
((Activity)context).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, strError, Toast.LENGTH_LONG).show();
}
});
}
return null;
}
}.execute();
}
private void DataListEducation() {
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... voids) {
try {
ConnectionOData4j connectionClass = new ConnectionOData4j(context);
ODataConsumer consumer = connectionClass.getConnection();
for(OEntity entity : consumer.getEntities("Education").execute()){
final String education=entity.getProperty("Education" , String.class).getValue();
((Activity) context).runOnUiThread(new Runnable() {
public void run() {
EmployeeEducation=education;
}
});
}
}catch (Exception e) {
final String strError=e.getMessage();
((Activity)context).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, strError, Toast.LENGTH_LONG).show();
}
});
}
return null;
}
}.execute();
}
Related
import com.github.nkzawa.emitter.Emitter;
private Emitter.Listener onNewMessage = new Emitter.Listener() {
#Override
public void call(final Object... args) {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
JSONObject data = (JSONObject) args[0];
String username;
String message;
try {
username = data.getString("username");
message = data.getString("message");
} catch (JSONExpextion e) {
return;
}
}
});
}
};
When I put in this code in my project, it said "Cannot resolve method
get Activity "
then, how can I deal with this problem?
You are calling getActivity() from inner class Emitter. So you should replace getActivity() with the name of your activity followed by .this. For example the name of your activity is MainActivity, then replace getActivity() with MainActivity.this
And your code would be like this:
private Emitter.Listener onNewMessage = new Emitter.Listener() {
#Override
public void call(final Object... args) {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
JSONObject data = (JSONObject) args[0];
String username;
String message;
try {
username = data.getString("username");
message = data.getString("message");
} catch (JSONExpextion e) {
return;
}
}
});
}
};
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();
}
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();
}
}
}
}
I'm trying to show a ProgressDialog from an AsyncTask but is not showing... if I delete the 'dialog.dismiss()' of the PostExecute, this task works like a 'post()', it shows the Dialog in the end of the task. I'm getting crazy with this!
private static class Experimento extends AsyncTask<String, Void, String>
{
ProgressDialog dialog;
#Override
protected void onPreExecute()
{
super.onPreExecute();
dialog = new ProgressDialog(Config.getCurrent_Context());
dialog.setMessage("Cargando...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
protected String doInBackground(String... params)
{
try
{
Config.getCurrent_Context().runOnUiThread(new Runnable()
{
#Override
public void run()
{
try
{
//Work, work, work...
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result)
{
super.onPostExecute(result);
dialog.dismiss();
}
}
Now tried giving the Activity in the constructor but still not working...
private static ProgressDialog dialog;
public Experimento(Activity act)
{
Log.w("Experimento", "Experimento");
dialog = new ProgressDialog(act);
dialog.setMessage("Cargando...");
dialog.setCancelable(false);
dialog.show();
}
#Override
protected void onPreExecute()
{
super.onPreExecute();
}
You should remove setIntermidiate(true);
Try this, it works for me:
Override
protected void onPreExecute()
{
dialog = new ProgressDialog(Config.getCurrent_Context());
dialog.setMessage("Cargando...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
protected String doInBackground(String... params)
{
String result = " ";
try
{
Config.getCurrent_Context().runOnUiThread(new Runnable()
{
#Override
public void run()
{
try
{
//Work, work, work...
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
catch(Exception e)
{
e.printStackTrace();
}
return result;
}
protected void onPostExecute(String result)
{
dialog.dismiss();
}
This error is Showing often "The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread."
Getting the NewsList and other images and ads from API using JSON.
class LongOperation extends AsyncTask<String, Void, String> {
protected void onPreExecute() {
}
protected String doInBackground(String... params) {
try {
AdSpotList.add(sched);
Image_view_List.add(sched);
NewsList.add(sched);
handler.sendEmptyMessage(1);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
}
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 1) {
if (NewsList.size() != 0) {
if (NewsList.size() <= 5) {
adapter = new CustomAdapter(getActivity(),
Image_view_List, NewsList, AdSpotList, res);
lv_daily_summary.setAdapter(adapter);
} else {
getActivity().runOnUiThread(new Runnable() {
public void run() {
lv_daily_summary.invalidateViews();
adapter.notifyDataSetChanged();
}
});
}
}
}
}
};
This complete page is on the Fragment.