vaadin: refreshing content after socket request - android

i am new to vaadin and am writing a websocket client with it connected with a websocket server on android.
the client generates a list of all phone-contact-groups on the left side, ad after each click on this group, the client loads the assoziated contacst to the right side of the computer screen.
verything works fine so far, execpt that the loaded contacts dont appear after the first click on it, but only after the second. it loads always the groups clicked before the actual click. in the debugger i can see that the right contacts are already loaded to the layout, but still they only appear after the next click. it always shows the "old contacts" not the ones that where created with the current click. i need somehow to force the layout to refresh, but without success so far
none of the answers i found so far helped: neither to use setImmediate(true), on the conainer, nor requestRepaint(true);
#Override
protected void init(VaadinRequest vaadinRequest) {
layoutMain = new HorizontalLayout();
setContent(layoutMain);
layoutLeft = new VerticalLayout();
layoutRight = new VerticalLayout();
layoutMain.addComponent(layoutLeft);
layoutMain.addComponent(layoutRight);
final Client client;
try {
client = new Client(new URI("ws://192.168.0.14:8080"));
Client.ClientListener clientListener = new Client.ClientListener() {
#Override
public void onOpen(ServerHandshake serverHandshake) {
short httpStatus = serverHandshake.getHttpStatus();
}
#Override
public void onMessage(String message) { // getting a json string with the data needed to fill the layout
try {
final JSONObject jsonObj = new JSONObject(message);
JsonHelper jsonHelper = new JsonHelper(jsonObj);
if (jsonHelper.isEvent("selectGroups")) {
JSONArray list = jsonHelper.getList();
for (int i = 0; i < list.length(); i++) {
JSONObject jsonGroup = (JSONObject) list.get(i);
final Button button = new Button(jsonGroup.getString("id") + ": " + jsonGroup.getString("name"));
button.setData(jsonGroup.getString("id"));
button.addClickListener(new Button.ClickListener() {
#Override
public void buttonClick(Button.ClickEvent clickEvent) {
try {
... // generate a json object to send to the server
client.send(jsonHelperClick.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
layoutLeft.addComponent(button); // adding the group as a button to the left side of the layout
}
// problem could be solved with push()
return;
}
if (jsonHelper.isEvent("availableContacts")) {
layoutRight.removeAllComponents();
JSONArray list = jsonHelper.getList(); // getting the list of all contacst from the phone
for (int i = 0; i < list.length(); i++) {
JSONObject jsonContact = (JSONObject) list.get(i);
HorizontalLayout rowContact = new HorizontalLayout();
... // filling the row representing the contact
layoutRight.addComponent(rowContact); // adding a contactrow to the right side of the layout
}
// problem could be solved with push()
// at this point i already see the right data added to layoutRight, but at the end still the old data is shown, not the new one
// layoutRight.requestRepaint(true);
// layoutRight.setImmediate(true);
// layoutRight.beforeClientResponse(true);
// layoutMain.requestRepaint(true);
// layoutMain.setImmediate(true);
//layoutMain.beforeClientResponse(true);
}
} catch (JSONException e) {
String message1 = e.getMessage();
e.printStackTrace();
}
}
};
client.setClientListener(clientListener);
client.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
EDIT:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>VaadinApplicationServlet</servlet-name>
<servlet-class>
com.vaadin.server.VaadinServlet</servlet-class>
<init-param>
<param-name>UI</param-name>
<param-value>at.richardlederer.RemoteSms</param-value>
</init-param>
<!-- Enable server push -->
<init-param>
<param-name>pushmode</param-name>
<param-value>manual</param-value>
</init-param>
<async-supported>false</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>VaadinApplicationServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
EDIT:
i changed the pushmode to manual, so that the changes only happen when i cal the push() method.

You need to enable server push in your vaadin application.
The onMessage callback handler is called later on,
when the json data is received by the application.
This then updates the UI state on serverside.
When the next interaction on the browser side is done,
all server side updates are then sent to the browser.
Enabling it is simple:
#Push
public class PushyUI extends UI {

calling the method push() after the components are filled solved the problem

Related

How do I send data from an android wearable device to a phone in the form of a a simple text file containing data?

I have a wearable app. The app after it finishes has data like time/date, UUID, Geo location, parameters selected displayed in front of me like a Data Report or Log in several TextViews underneath each other. Like a list. I want this data to be transferred from my wearable device to my android phone.
Now I have to ask does the WearOS app the pairs the phone with the watch enables such a thing? Like can the data be sent through it? OR what exactly can I do? I read about Sync data items with the Data Layer API in the documentation, but I'm not sure if the code snippets provided would help achieve what I want.
public class MainActivity extends Activity {
private static final String COUNT_KEY = "com.example.key.count";
private DataClient dataClient;
private int count = 0;
...
// Create a data map and put data in it
private void increaseCounter() {
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
putDataMapReq.getDataMap().putInt(COUNT_KEY, count++);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
Task<DataItem> putDataTask = dataClient.putDataItem(putDataReq);
}
...
}
The data I display in the textviews are called through methods that I call things like: getLocation, getUUID, getDateTime, getSelections, etc... when I click a button I call them in the setOnClickListener. I want this data in the TextViews to be placed in a file or something like that and send them over to the mobile phone from the watch when they're generated.
private void getDateTime()
{
SimpleDateFormat sdf_date = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat sdf_time = new SimpleDateFormat("HH:mm:ss z");
String currentDate= sdf_date.format(new Date());
String currentTime= sdf_time.format(new Date());
textView_date_time.setText("Date: "+currentDate+"\n"+"Time: "+currentTime);
}
#SuppressLint("SetTextI18n")
private void getUUID()
{
// Retrieving the value using its keys the file name
// must be same in both saving and retrieving the data
#SuppressLint("WrongConstant") SharedPreferences sh = getSharedPreferences("UUID_File", MODE_APPEND);
// The value will be default as empty string because for
// the very first time when the app is opened, there is nothing to show
String theUUID = sh.getString(PREF_UNIQUE_ID, uniqueID);
// We can then use the data
textView_UUID.setText("UUID: "+theUUID);
}
#SuppressLint("SetTextI18n")
private void getSelections()
{
textView_data_selected.setText("Tool No.: "+c.getToolNo()+
"\nTool Size: " +c.getToolSizeStr()+
"\nFrom Mode: " +c.getCurrentModeStr()+
"\nGoto Mode: " +c.getModeStr()+
"\nMethod: " +c.getMethodStr()+
"\nBit Duration: " +c.getBitDuration()+
"\nUpper bound" +c.getUpStageValue()+
"\nLower bound: "+c.getDownStageValue());
}
The above are examples of the methods I use to get the data. then I call them here:
gps_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= 26) {
getLocation();
getDateTime();
getUUID();
getSelections();
}
else
{
//ActivityCompat.requestPermissions(get_location.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
Toast.makeText(get_location.this,"Build SDK too low",Toast.LENGTH_SHORT);
}
}
});
Now how do I take all this and send it over from my device to the the phone?
Note: The data report I want to send as a file, I want it done subtly like something done in the background. I don't know what else to do or where to look.
You have two options if you want to use the Data Layer, one is to use the MessageClient API to bundle your data up in a message and send it directly to the handheld. The easiest here would be to create an arbitrary JSONObject and serialize your data as a JSON string you can stuff into a message. For example:
try {
final JSONObject object = new JSONObject();
object.put("heart_rate", (int) event.values[0]);
object.put("timestamp", Instant.now().toString());
new MessageSender("/MessageChannel", object.toString(), getApplicationContext()).start();
} catch (JSONException e) {
Log.e(TAG, "Failed to create JSON object");
}
In my case, I do this in my onSensorChanged implementation, but you can insert this wherever you are updating your text.
MessageSender is just a threaded wrapper around the MessageClient:
import java.util.List;
class MessageSender extends Thread {
private static final String TAG = "MessageSender";
String path;
String message;
Context context;
MessageSender(String path, String message, Context context) {
this.path = path;
this.message = message;
this.context = context;
}
public void run() {
try {
Task<List<Node>> nodeListTask = Wearable.getNodeClient(context.getApplicationContext()).getConnectedNodes();
List<Node> nodes = Tasks.await(nodeListTask);
byte[] payload = message.getBytes();
for (Node node : nodes) {
String nodeId = node.getId();
Task<Integer> sendMessageTask = Wearable.getMessageClient(context).sendMessage(nodeId, this.path, payload);
try {
Tasks.await(sendMessageTask);
} catch (Exception exception) {
// TODO: Implement exception handling
Log.e(TAG, "Exception thrown");
}
}
} catch (Exception exception) {
Log.e(TAG, exception.getMessage());
}
}
}
The other option is to create a nested hierarchy of data items in the Data Layer and implement DataClient.OnDataChangedListener on both sides, such that changes that are written in on one side are automatically synchronized with the other. You can find a good walkthrough on how to do that here.
For your specific case, just packing it in a JSON object would probably be the simplest. The writing out to your preferred file format you can then implement on the handheld side without needing to involve the wear side.

Android Volley request weird "1 second" delay between multiple fast clicks

Sorry for my bad english, but i have problem and can't figure it out..
I have custom listview in my fragment for getting products from JSON array. And I have 3 ImageButtons in every listview row;
Plus Button(+), Minus Button(-) and Remove Button(X)..
So when i click each button, its calling JSON request for update product's piece number, getting new datas from response and repopulate array list.
Everything ok but, when I click that buttons faster, it seems there is 1 sec delay between multiple requests even first volley request has already done before.
Here is my JSON method in TableAdapter.java
public void JSON(final int position, final int process) {
if (inProgress==false) {
dialog = new ProgressDialog(myContext);
dialog.setMessage("Updating.....");
dialog.show();
inProgress = true;
System.out.println("**** Now request is beginning............");
final int rowID = tableModelList.get(position).getID();
final int treeID = tableModelList.get(position).getAna_dal();
final float price = tableModelList.get(position).getBFiyat();
final int quantity = tableModelList.get(position).getAdet();
final int print = tableModelList.get(position).getYazdimi();
final String insertUrl = "http://"+getServer+"/t6mobilservice/order-piece.php";
Map<String, String> parameters = new HashMap<>();
parameters.put("tableID", "" + getTableId);
parameters.put("rowID", "" + rowID);
parameters.put("treeID", ""+treeID);
parameters.put("process",""+process);
parameters.put("quantity",""+quantity);
parameters.put("price",""+price);
parameters.put("print",""+print);
CustomRequest jsObjRequest = new CustomRequest(Request.Method.POST, insertUrl, parameters, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray getOrders = response.getJSONArray("order");
Gson gson = new Gson();
tableModelList.clear();
for (int i = 0; i < getOrders.length(); i++) {
JSONObject order = getOrders.getJSONObject(i);
TableModel tableModel = gson.fromJson(order.toString(), TableModel.class);
tableModelList.add(tableModel);
}
mAdapter.notifyDataSetChanged();
System.out.println("**** onResponse: Request is done............");
System.out.println("**** JSON: "+response.toString());
dialog.cancel();
inProgress = false;
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError response) {
Log.d("Response: ", response.toString());
inProgress = false;
}
});
requestQueue.add(jsObjRequest);
}
}
Dialog is showing when request beginning and canceling when request is done. But as I said, when I click faster, there is a delay between multiple requests even first request has already done. I'm saying 1 sec, because 2nd request is beginning after 1 sec exactly, no matter how I clicking fast. I can't figure it out..
I tested fast clicks to showing toasts with random numbers and it's working fine. But when I testing with JSON method, there is a weird delay.
I have SwipeRefreshLayout and ScrollView in my fragment.
I'm using Volley with singleton.
EDIT 1. : Here's a video link to showing what my problem is..
EDIT 2. : Here's a LogCat output video for every click
Every fast click has returning success json output, but nothing change instantly..
Thanks for your help.

Android Facebook Change/Set new AccessToken

I am trying to allow a user to login as a page with facebook - so if there's an easier way of doing it over what I'm trying, please feel free to tell me -
Currently, I log the user in with LoginManager and get the details of the user (i.e. their page and page access_token) using a GraphRequest. I then put the user's pages into a spinner so the user can select which page to post as (including their own).
Here's the problem. I have the access_token as a string from when the user logs in and I pull the information and I see the access_token when the user selects the page they want, but I'm totally unsure on how to set a new access token, especially from a string.
Here's the code I'm using:
GraphRequest requestPage = GraphRequest.newGraphPathRequest(
currentAccessToken,
"/me/accounts",
new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
JSONArray jsonArray = null;
try {
jsonArray = response.getJSONObject().getJSONArray("data");
for(int i=0; i < jsonArray.length(); i++){
JSONObject page = jsonArray.getJSONObject(i);
String pageName = page.getString("name");
String pageToken = page.getString("access_token");
if(!pageName.equals("")) {
//I know there's a better way of getting the name and token together in an array, but I don't know how
pages_array.add(pageName);
pages_token_array.add(pageToken);
facebook_pages.setVisibility(View.VISIBLE);
} else {
pages_array.clear();
pages_token_array.clear();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(StartPage.this, android.R.layout.simple_spinner_item, pages_array);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spinnerArrayAdapter.notifyDataSetChanged();
page_spinner.setAdapter(spinnerArrayAdapter);
}
});
Bundle pageParameters = new Bundle();
pageParameters.putString("fields", "name,access_token,picture{url}");
requestPage.setParameters(pageParameters);
requestPage.executeAsync();
);
Then here's how I select the spinner
facebook_select_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String spinner_value = page_spinner.getSelectedItem().toString();
int spinner_position = page_spinner.getSelectedItemPosition();
String newAccessToken = pages_token_array.get(spinner_position);
//AccessToken token = new AccessToken(newAccessToken);
//TODO: SET NEW ACCESS TOKEN
};
});
Like I said, I have the selection working and I can see the access token if I were to log it or Toast it on the button press. I just don't know how to CHANGE The access token.
Any ideas?

Get current users like status on posts

I have been able to pull the Facebook newsfeed for the logged in user using the "me/home" Graph API Call and am displaying the result in an activity.
Now I have been trying numerous methods which will query the post's like column and check if the current user has liked a post to display the status. Essentially, I am setting a drawable to indicate the user has liked a post.
I cannot really post any code that I have tried so far simply because none of them work. And believe me, I have tried literally numerous methods.
I would appreciate if someone could at least prod me in the right direction.
EDIT: This is my latest attempt at querying the likes column and comparing the result with the current user's ID:
This code is where I am checking for likes count and adding them to an ArrayList. This is also where I am running the query to get the likes on the post.
// GET THE POST'S LIKES COUNT
if (json_data.has("likes")) {
JSONObject feedLikes = json_data.optJSONObject("likes");
String countLikes = feedLikes.getString("count");
postLikesCountArrayList.add(countLikes);
// TEST STARTS
// QUERY THE LIKES COLUMN TO CHECK YOUR LIKE STATUS ON A POST
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, Utility.mFacebook.getAccessToken());
Utility.mAsyncRunner.request(finalThreadID + "/likes&limit=200", params, new LikesListener());
// TEST ENDS
} else {
String countLikes = "0";
postLikesCountArrayList.add(countLikes);
}
And this code block is the Listener (a privte class in the same activity) where the results are checked:
private class LikesListener extends BaseRequestListener {
#Override
public void onComplete(final String response, final Object state) {
try {
JSONObject JOLikes = new JSONObject(response);
JSONArray JALikes = JOLikes.getJSONArray("data");
for (int i = 0; i < JALikes.length(); i++) {
JSONObject JOTemp = JALikes.getJSONObject(i);
if (JOTemp.has("id")) {
String getTempID = JOTemp.getString("id");
if (getTempID.equals(initialUserID)) {
Runnable run = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
ImageView postYourLike = (ImageView) findViewById(R.id.postYourLike);
postYourLike.setBackgroundResource(R.drawable.btn_icon_liked);
}
};
TestNewsFeeds.this.runOnUiThread(run);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
You can use this fql. Replace the post_id
SELECT likes.can_like, likes.user_likes FROM stream WHERE post_id = "1274834235_3976149403543"
response would look like this, if user_likes is true, he has liked it
{
"data": [
{
"likes": {
"can_like": true,
"user_likes": false
}
}
]
}

How to get comments of Facebook Notes Items using several Graph API in android app?

I want to show Facebook Page's Notes items with those comments and likes using Graph API.
To do that, I'm using the asyncFacebookRunner in Facebook SDK.
Steps are like this:
call asyncFacebookRunner.request to get Note Item with PageId
mAsyncRunner.request(sAPIString, new NotesRequestListener(), null);
Response has come. ( I can't highlight function call. Sorry for inconvenient to find it.)
public class NotesRequestListener implements com.facebook.android.AsyncFacebookRunner.RequestListener
{
/**
* Called when the request to get notes items has been completed.
* Retrieve and parse and display the JSON stream.
*/
#Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
Log.i("My_TAG", "onComplete with response, state");
try
{
// process the response here: executed in background thread
final JSONObject json = new JSONObject(response);
JSONArray arrNotesItems = json.getJSONArray("data");
int l = (arrNotesItems != null ? arrNotesItems.length() : 0);
// !!!!
// This has another request call
// !!!!
final ArrayList<WordsItem> newItems = WordsItem.getWordsItems(arrNotesItems,getActivity());
WordsActivity.this.runOnUiThread(new Runnable() {
public void run() {
wordsItems.clear();
wordsItems.addAll(newItems);
aa.notifyDataSetChanged();
}
}); // runOnUiThread
} // try
catch (JSONException e)
{
Log.i("My_TAG", "JSON Error in response");
} // catch
} // onComplete
... other override methods ...
} // Request Listener
< Another Class >
public static ArrayList<WordsItem> getWordsItems(JSONArray arrJSON, Activity activity) {
ArrayList<WordsItem> wordsItems = new ArrayList<WordsItem>();
int l = (arrJSON != null ? arrJSON.length() : 0);
try {
WordsItem newItem;
for (int i=0; i<l; i++) {
JSONObject jsonObj = arrJSON.getJSONObject(i);
String sTitle = jsonObj.getString("subject");
String sNoteID = jsonObj.getString("id");
... get another fields here ...
newItem = new WordItem(...);
// !!!!
// This has request call for comments
// !!!!
ArrayList<CommentItem> arrComment = getUserComments(sNoteID);
wordsItems.add(newItem);
}
} catch (Exception e) {
e.printStackTrace();
}
return wordsItems;
} // getWordsItems
call another asyncFacebookRunner.request to get comments of item(with NoteID)
in getUserComments
mAsyncRunner.request(sAPIString, new CommentRequestListener(), null);
Before getting comments(OnComplete in CommentRequestListener has not called), getWordsItems returns item array.
So I can't see the comments.
How can I wait to update UI till getting comments?
(It's so ironic to synchronize asynchronized calls.)
Thanks in advance.
Use facebook object which has non-asynchronous request method.
You need not implement listener method.
So, I suggest below means.
use mAsyncRunner.request for first request call.
use mFacebookRunner.request for second request call.
I hope it may help you:-)
Using FQL - Facebook Query Language you can easily get all this information about any particular note info
, Also to get likes on that and comments over it as like examples given in the links.

Categories

Resources