Android Socket.io emits twice instead of once? - android

I'm using android socket.io but the problem is that emit event runs twice instead of once , i mean that i wrote just one emit code in the onCreate method , but it sends two request for the server ?
I searched alot but not found anything .
I use node js in the backend ,and my code has not any problems .
Is there a bug in socket.io for android ?
Here is my code :
SocketManager.getInstance().connect();
// Creating Bids
final Handler mHandler04 = new Handler(Looper.getMainLooper());
mHandler04.post(new Runnable() {
#Override
public void run() {
SocketManager.getInstance().getSocket().emit("allc", "some");
SocketManager.getInstance().getSocket().on("allcRes", new Emitter.Listener() {
#Override
public void call(final Object... args) {
g.context.runOnUiThread(new Runnable() {
#Override
public void run() {
JSONArray jsonArray = (JSONArray) args[0];
arrayComps.clear();
Log.d(TAG, "run: " + jsonArray);
try {
for (int i = 0; i < jsonArray.length(); i++) {
createView(jsonArray.getJSONObject(i).getString("title"), jsonArray.getJSONObject(i).getString("realprice"), jsonArray.getJSONObject(i).getString("id"), jsonArray.getJSONObject(i).getString("starttime"), jsonArray.getJSONObject(i).getString("img"));
CustomViewCompetition css = (CustomViewCompetition) LinearLayoutItemHolder.getChildAt(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
});
final Handler bidsupdateHandler = new Handler(Looper.getMainLooper());
bidsupdateHandler.post(new Runnable() {
#Override
public void run() {
SocketManager.getInstance().getSocket().on("bidsupdate", new Emitter.Listener() {
#Override
public void call(final Object... args) {
final JSONArray jsonArray = (JSONArray) args[0];
g.context.runOnUiThread(new Runnable() {
#Override
public void run() {
Log.d(TAG, "run in bidsupdate");
try {
for (int i = 0; i < jsonArray.length(); i++) {
bidsMap.put(jsonArray.getJSONObject(i).getInt("key"), jsonArray.getJSONObject(i).getInt("value"));
}
for (int i = 0; i < LinearLayoutItemHolder.getChildCount(); i++) {
CustomViewCompetition cs = (CustomViewCompetition) LinearLayoutItemHolder.getChildAt(i);
Log.d(TAG, "run in cs " + cs.txtCsRealPrice.getText());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
});
the Problem is when i get the cs.txtCsTitle.getText() it is just showing me one of them

Add this line:
SocketManager.getInstance().getSocket().emit("allc", "some");
just after:
SocketManager.getInstance().connect();
So your code will be like
SocketManager.getInstance().connect();
SocketManager.getInstance().getSocket().emit("allc", "some");
And remove from where you are already using in handler. Also you dont need handlers why you are using handlers? Create your listener and emit your subscription out of handlers it needs to be created or subscribed once. On creation of emitter listener pass views if you need to update any view. then update the views here you may need runnable to update UI on uiThread.
Create Class as follows:
public class YourListener implements Emitter.Listener
{
private yourView view;
public YourListener (View view)
{
this.view = view;
}
#Override
public void call(Object... args)
{
// do your work here
}
}
and replace below code :
SocketManager.getInstance().getSocket().on("allcRes", new Emitter.Listener() {
#Override
public void call(final Object... args) {
g.context.runOnUiThread(new Runnable() {
#Override
public void run() {
JSONArray jsonArray = (JSONArray) args[0];
arrayComps.clear();
Log.d(TAG, "run: " + jsonArray);
try {
for (int i = 0; i < jsonArray.length(); i++) {
createView(jsonArray.getJSONObject(i).getString("title"), jsonArray.getJSONObject(i).getString("realprice"), jsonArray.getJSONObject(i).getString("id"), jsonArray.getJSONObject(i).getString("starttime"), jsonArray.getJSONObject(i).getString("img"));
CustomViewCompetition css = (CustomViewCompetition) LinearLayoutItemHolder.getChildAt(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
with:
SocketManager.getInstance().getSocket().on("your_subscription", new YourListener (yourView));

Related

Bolts Framework: onSuccess make changes on UI/Main thread

Is it possible to display a message or make changes on the UI thread once Task callInBackground is finished executing?
Something like following:
Task.callInBackground(new Callable<String>() {
#Override
public String call() {
for(int i=0; i<3; i++){
Log.i("I=", String.valueOf(i));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String obj = "";
return null;
}
}).onSuccess(new Continuation<String, Object>() {
#Override
public Object then(Task<String> task) throws Exception {
Log.i("I=", "Counter complete");
Toast.makeText(MainLoanMemberActivity.this, "Finished", Toast.LENGTH_SHORT).show();
btnAgriLoan.setText("LOL");
return null;
}
});
At the moment there is no Toast message displayed and there is no crash as well.
Looking for an equivalent of AsyncTask's onPostExecute in Bolts Framework where one can add changes to UI.
Didn't realized that there are EXECUTOR's types that you can mentioned with each helper function like so: (Task.UI_THREAD_EXECUTOR)
Task.callInBackground(new Callable<String>() {
#Override
public String call() {
for(int i=0; i<3; i++){
Log.i("I=", String.valueOf(i));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String obj = "";
return null;
}
}).onSuccess(new Continuation<String, Void>() {
public Void then(Task<String> object) throws Exception {
Toast.makeText(MainLoanMemberActivity.this, "Finished", Toast.LENGTH_SHORT).show();
btnAgriLoan.setText("LOL");
return null;
}
}, Task.UI_THREAD_EXECUTOR);
Docs helped!

AsyncHttpPost force close when there is not internet

I used fragment in my application and when there is not internet app getting force close . I don't know what can I do . please help me
this code works perfect but when internet lost the application get error and force close
i use method to check internet by ping but it makes my app too slow
public static void get_detail(final int pages){
MainActivity.logo11.setImageResource(R.drawable.loading);
AsyncHttpPost post = new AsyncHttpPost(
"domain"
);
post.setTimeout(30000);
MultipartFormDataBody body = new MultipartFormDataBody();
body.addStringPart("City",MainActivity.sp.getString("City",null));
body.addStringPart("Cate","all");
body.addStringPart("Page", String.valueOf(pages));
post.setBody(body);
AsyncHttpClient.getDefaultInstance().executeString(post, new AsyncHttpClient.StringCallback() {
#Override
public void onCompleted(final Exception e, AsyncHttpResponse source, final String result) {
if(e != null){
MainActivity.activity.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(MainActivity.activity,"network error",Toast.LENGTH_LONG).show();
refresh.setRefreshing(false);
e.printStackTrace();
}
});
}
if(!result.equals("")){
MainActivity.activity.runOnUiThread(new Runnable() {
#Override
public void run() {
MainActivity.logo11.setImageResource(R.drawable.logo);
if(page==0){
hash_all.clear();
}
items.clone();
try {
JSONArray jsonArray = new JSONArray(result);
for (int i = 0 ;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
HashMap<String , Object> hash_add = new HashMap<String, Object>();
hash_add.put("ID",object.getString("ID"));
hash_add.put("Username",object.getString("Username"));
hash_add.put("Title",object.getString("Title"));
hash_add.put("Descript",object.getString("Descript"));
hash_all.add(hash_add);
items = new String[hash_all.size()];
}
ad.notifyDataSetChanged();
refresh.setRefreshing(false);
}catch (Exception e){
e.printStackTrace();
}
}
});
}else {
Toast.makeText(MainActivity.activity,"error",Toast.LENGTH_LONG).show();
}
}
});
}

best way to run a method inside service without disturbing Main UI thread

I have a method which fetches the contacts from phone and send this to the php server for processing then it will return data so that it will update in the sql lite DB. I need to run this method continuously on background. I am using Volley for network operation. I am using Handler inside service to run this method. The problem is i am seen too many skipping frames and app is very slow and stucks. I want to run this method without disturbing the main thread. The service code is mentioned below.
public class serv extends Service {
ArrayList<String> aa= new ArrayList<>();
ArrayList<String> bb= new ArrayList<>();
JSONObject JSONimdb;
JSONObject EverythingJSON;
ArrayList<mobstat> musers = new ArrayList<mobstat>();
private RequestQueue requestQueue;
String l;
private static Timer timer = new Timer();
#Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("Shiva","Service Killed");
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
//nonstop1();
threadcheck();
return Service.START_STICKY;
}
private void _startService()
{
long UPDATE_INTERVAL = 5 * 1000;
timer.scheduleAtFixedRate(
new TimerTask()
{
public void run()
{
try
{
getNumber(serv.this.getContentResolver());
} catch (JSONException e)
{
e.printStackTrace();
}
}
}, 1000, UPDATE_INTERVAL);
}
private void nonstop1()
{
final Handler handlera = new Handler();
Runnable updatea = new Runnable()
{
#Override
public void run()
{
try {
getNumber(serv.this.getContentResolver());
} catch (JSONException e) {
e.printStackTrace();
}
handlera.postDelayed(this , 1000);
}
};
handlera.postDelayed(updatea, 10);
}
private void threadcheck()
{
new Thread() {
public void run() {
try {
Log.e("Shiva","Threadcheck");
getNumber(serv.this.getContentResolver());
} catch (JSONException e) {
e.printStackTrace();
}
}
}.start();
}
#Override
public void onDestroy()
{
super.onDestroy();
// Intent broadcatIntent = new Intent("com.statmob.findnum");
//sendBroadcast(broadcatIntent);
//stoptimertask();
//nonstop1();
threadcheck();
}
public void getNumber(ContentResolver cr) throws JSONException
{
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (phoneNumber.length()>=10)
{
l = phoneNumber.substring(phoneNumber.length()-10);
aa.add(l);
bb.add(name);}
}
phones.close();
JSONimdb = new JSONObject();
for (int i = 0; i < aa.size(); i++)
{
try
{
JSONimdb.put(bb.get(i), aa.get(i));
} catch (JSONException e)
{
e.printStackTrace();
}
}
EverythingJSON = new JSONObject();
try
{
EverythingJSON.put("imdblist", JSONimdb);
} catch (JSONException e)
{
e.printStackTrace();
}
StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://xxxxxxxx/cont.php",
new Response.Listener<String>()
{
#Override
public void onResponse(String s)
{
if (s != null)
{
parseJSONresponse(s);
}
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error)
{
Log.e("Shiva",""+error);
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError
{
Map<String, String> params = new Hashtable<String, String>();
params.put("arr", EverythingJSON.toString());
return params;
}
};
int socketTimeout = 50000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
if(requestQueue==null)
{
requestQueue = Volley.newRequestQueue(serv.this);}
stringRequest.setRetryPolicy(policy);
requestQueue.add(stringRequest);
}
private void parseJSONresponse(String s)
{
try
{
JSONArray json = new JSONArray(s);
for (int i = 0; i < json.length(); i++)
{
JSONObject e = json.getJSONObject(i);
musers.add(new mobstat(e.getString("name"), e.getString("status"),e.getLong("time")));
SugarRecord.updateInTx(musers);
}
} catch (JSONException e)
{
e.printStackTrace();
}
}
}
I have tried Timer & Handler, these are working fine but the it makes app slow and not responding. Thread is not working. Please suggest some better way to run this method in background without any disturb to main thread.
create separate process for your service in manifest file might this help you
<manifest>
....
<application>
.....
<service android:name=".serv" android:process=":myprocess" >
</application>
</manifest>
Have you tried IntentService? It's an easy way of running time-consuming background task.
Modify your current implementation with IntentServices.
By default IntentServices runs in background thread. If using Services it uses main thread by default. Then you need to run a Thread inside service to make that execution block in background.
Go through this and this link for more information

Thread not moving to notify after wait is being called

I have this below piece of code, I want to my thread to wait untill either of the callback functions are called.
Issue my code hits the line where i am making the synchObj wait but after that it just vanishes it doesn't move anywhere.
If after wait its not gonna move ahead how the notify will be called?
iGPlaceApi.getStreams(new Callback<mGooglePlacesApiResponse>() {
#Override
public void failure(RetrofitError retrofitError) {
String serverResponse = retrofitError.toString();
synchronized (synchObj) {
synchObj.notify();
//synchObj.notifyAll();
}
}
#Override
public void success(mGooglePlacesApiResponse googlePlacesObj, Response arg1){
nearbyPlaces = new String[googlePlacesObj.results.size()][4];
for (int i = 0; i < googlePlacesObj.results.size(); i++) {
mGooglePlaces.place place = googlePlacesObj.results.get(i);
nearbyPlaces[i][0] = place.icon;
nearbyPlaces[i][1] = place.name;
nearbyPlaces[i][2] = String.valueOf(place.geometry.location.lat);
nearbyPlaces[i][3] = String.valueOf(place.geometry.location.lng);
}
synchronized (synchObj) {
synchObj.notify();
//synchObj.notifyAll();
}
}
});
synchronized (synchObj) {
synchObj.wait();
}
Handler handler=new Handler();
Runnable thr = new Runnable() {
public void run() {
iGPlaceApi.getStreams(new Callback<mGooglePlacesApiResponse>() {
#Override
public void failure(RetrofitError retrofitError) {
String serverResponse = retrofitError.toString();
synchronized (synchObj) {
synchObj.notifyAll();
}
}
#Override
public void success(mGooglePlacesApiResponse googlePlacesObj, Response arg1) {
nearbyPlaces = new String[googlePlacesObj.results.size()][4];
for (int i = 0; i < googlePlacesObj.results.size(); i++) {
mGooglePlaces.place place = googlePlacesObj.results.get(i);
nearbyPlaces[i][0] = place.icon;
nearbyPlaces[i][1] = place.name;
nearbyPlaces[i][2] = String.valueOf(place.geometry.location.lat);
nearbyPlaces[i][3] = String.valueOf(place.geometry.location.lng);
}
synchronized (synchObj) {
synchObj.notifyAll();
}
}
});
}
};
handler.post(thr);
synchronized (synchObj) {
synchObj.wait();
}
You can't do that. The callbacks are going to be called on the same thread that is calling the getStreams method.
The callbacks cannot be called until your calling method returns. You probably need to call getStreams in yet another thread.
You are doing it wrong. Handler task will be executed on the same thread so in your case it will never be executed. If you want to wait for your task to be done you should use Thread like this:
// defined somewhere
boolean done = false;
Thread thr=new Thread(Runnable() {
public void run() {
iGPlaceApi.getStreams(new Callback<mGooglePlacesApiResponse>() {
#Override
public void failure(RetrofitError retrofitError) {
String serverResponse = retrofitError.toString();
synchronized (synchObj) {
done = true;
synchObj.notifyAll();
}
}
#Override
public void success(mGooglePlacesApiResponse googlePlacesObj, Response arg1) {
nearbyPlaces = new String[googlePlacesObj.results.size()][4];
for (int i = 0; i < googlePlacesObj.results.size(); i++) {
mGooglePlaces.place place = googlePlacesObj.results.get(i);
nearbyPlaces[i][0] = place.icon;
nearbyPlaces[i][1] = place.name;
nearbyPlaces[i][2] = String.valueOf(place.geometry.location.lat);
nearbyPlaces[i][3] = String.valueOf(place.geometry.location.lng);
}
synchronized (synchObj) {
done = true;
synchObj.notifyAll();
}
}
});
}
});
thr.start();
synchronized (synchObj) {
while (!done) {
synchObj.wait();
}
}
Note that instead of just runnable I'm using Thread, and done variable which indicates task status which is neccesary since wait call can spontaneously wake up and you have to call it again if the task has not been finished yet.

ProgressDialog does not want to update the message

I just tried to implement a progressdialog and I have some issues to change the text during my long and complex calculations.
for (String aString:myStringArray){
Log.v(TAG, aString);
mProgressDialog.incrementProgressBy(1);
mProgressDialog.setMessage(aString);
}
I can clearly see the incrementProgressBy working and my dialog updating, but the message does not change.
Any idea on how to make that work?
Thank a lot.
Just found the answer, that's working fine:
runOnUiThread(changeMessage);
with that code:
private Runnable changeMessage = new Runnable() {
#Override
public void run() {
//Log.v(TAG, strCharacters);
m_ProgressDialog.setMessage(strCharacters);
}
};
I upload pictures to Firebase in a loop and updating the ProgressDialog each image:
(I am in a Fragment, so I use getActivity() before calling to runOnUiThread())
List<Bitmap> bitmaps;
int picCounter = 1;
...
progressDialog = ProgressDialog.show
(getContext(), "sending...", "just a minute", false, false);
new Thread(new Runnable() {
#Override
public void run() {
for (int i = 0; i < bitmaps.size(); i++) {
String filename = String.valueOf(i);
uploadPic(bitmaps.get(i), "img" + filename, new MyCallback() {
#Override
public void onFinish() {
picCounter++;
Objects.requireNonNull(getActivity()).runOnUiThread(new Runnable() {
public void run() {
progressDialog.setTitle ("upoading " + picCounter + "image from " + bitmaps.size());
}
});
}
});
}
}
}).start();
uploadPic method:
public interface MyCallback { void onFinish ();}
private void uploadPic(final Bitmap bitmap, final String fileName, final MyCallback callback) {
... // uploading to firebase and then:
callback.onFinish();
}

Categories

Resources