i am using openfire xmpp server and asmack library to implement pubub node , the biggest issue that i face is the reconnection problem to the xmpp server.
whenever a connection is made to the xmpp server there are lots of stanza is being exchanged possibly around 5 to 7 stanza in a desktop applicaiton or web application it seems ok , but for mobile application where 3G link is pretty weak , in that case it too good to exchange so much of stanza.
i have tested that when the wifi has a weak signal or 3g link is down , i am making a reconnecting to the server (in background process) but i mostly get a response time out error and taking too much time if the connection gets successful.
i have seen messenger like whatsapp in which they are making a reconnection pretty fast. i want to do something like that only.
i have read about the pre-http binding but its exist in ejabber but didnt find anything in openfire moreover htt-prebinding is for anonymous users and i am using registered users only.
so can anyone tell me how can i reconnect fast to the xmpp server.
code snippet
Boolean onlyConnect = arg[0];
try {
if(xmpp!= null && !xmpp.isConnected())
xmpp.connect();
} catch(BOSHTimeoutException e){
Log.e("XConnection " , "Timeout occures");
// make a reconnection here
return false;
}
catch(ConnectionException e){
CatchErrors("ConnectionException" , "xmpp.connect()");
e.printStackTrace();
}catch(NoResponseException e){
CatchErrors("NoResponseException" , "xmpp.connect()");
// make a reconnection here
return false;
}catch (XMPPException e1) {
// TODO Auto-generated catch block
CatchErrors("XMPPException" , "xmpp.connect()");
e1.printStackTrace();
return false;
} catch (SmackException e) {
CatchErrors("SmackException" , "xmpp.connect()");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
CatchErrors("IOException" , "xmpp.connect()");
e.printStackTrace();
}
try {
Log.e("LOGIN REQUIRED " , "LOGIN IN");
xmpp.login(XMPP_USER_NAME, XMPP_PASSWORD, "sh");
} catch (XMPPException e) {
// TODO Auto-generated catch block
CatchErrors("XMPPException" , "xmpp.login()");
e.printStackTrace();
return false;
} catch(NoResponseException e){
Log.e("LOGIN REQUIRED " , "NoResponseException");
CatchErrors("NoResponseException" , "xmpp.login()");
//make a reconnection here
return false;
}catch (SaslException e) {
// TODO Auto-generated catch block
CatchErrors("SaslException" , "xmpp.login()");
e.printStackTrace();
} catch (SmackException e) {
// TODO Auto-generated catch block
CatchErrors("SmackException" , "xmpp.login()");
e.printStackTrace();
} catch (IOException e) {
CatchErrors("IOException" , "xmpp.login()");
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
Related
I want to disable data connection and I am using this code.
ConnectivityManager dataManager;
dataManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
try {
dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dataMtd.setAccessible(true);
try {
dataMtd.invoke(dataManager, false);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this will not work in dual sim. so can someone help me.
how can I disable data connection in dual sim android phone?
and how can i check if phone is dual sim or not
Some things to mention here:
before Android 5.1 there was no official API for dual sim functionality. Thus there seems to be no universal solution for older devices. Since 5.1 an API is available.
your code will no longer work on Android 5+, as pointed out in this question
however, this answer provides a solution to both bullets above: the method setMobileNetworkfromLollipop checks whether target is 5 or 5.1+, and in case of 5.1+ it loops through all subscription id's (=sim cards) to switch data services. You could combine it with your code to target previous versions as well. The downside: it requires root access, and dual-sim functionality is limited to 5.1+.
Hello friends I want to post comment to particular post in facebook with my android app, below is my code-
I Fetch All comments from URL which is below
http://retirementhomeslisting.com/home-detail/canterbury-place-83
Bundle mBundle=new Bundle();
mBundle.putString("message","testing...1234555");
#SuppressWarnings("deprecation")
Facebook fb=new Facebook(getResources().getString(R.string.fb_app_id));
try {
fb.request("478409145610921/comments",mBundle,"POST");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here I provide one link which you can see all comment list with respective above object_id
Link for comment list for 478409145610921 object_id
When i run above code comment is not post with respective object_id so Any idea how can i solve it?
EDIT Answer
Bundle mBundle=new Bundle();
mBundle.putString("message","testing...1234555");
#SuppressWarnings("deprecation")
Facebook fb=new Facebook(getResources().getString(R.string.fb_app_id));
try {
fb.request("1309634065_478409145610921/comments",mBundle,"POST");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
To post a comment on a post, you need a post_id instead of object_id.
So your API call must be-
/{post_id}/comments
(post_id is also a field in the comments table)
Edit:
You must be using a comment plugin and trying to post a comment to that. Unfortunately, facebook don't allows you to do that! (Logical- since most of them are public, to avoid spamming, else anyone could flood comments to that post)
If you check the response here, you'll get:
{
"error": {
"message": "(#100) Comments may not be added to a comment plugin",
"type": "OAuthException",
"code": 100
}
}
You can test these API call on Graph API Explorer also.
Using Graph API:
...
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"publish_actions"]) {
[[[FBSDKGraphRequest alloc]
initWithGraphPath:#"post_id/likes"
parameters: #{ #"like" : #"true"}
HTTPMethod:#"POST"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"It was liked.");
}
}];
}
...
Error:
Response={ "error": { "message": "(#100) Requires extended permission: publish_checkins or publish_actions", "type": "OAuthException", "code": 100 }}
Source Code:
Bundle params1 = new Bundle();
params1.putString("access_token",token);
params1.putString("place", "566414493392930"); // YOUR PLACE ID
params1.putString("message","I m here in this place");
JSONObject coordinates = new JSONObject();
try {
coordinates.put("latitude", "30.902823300000000000");
coordinates.put("longitude", "75.830516900000020000");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
params1.putString("coordinates",coordinates.toString());
params1.putString("tags",params[0]);//where xx indicates the User Id
String response="";
try {
response =faceBook.request("me/checkins", params1, "POST");
Log.v("Response","Response="+response);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Well, the error says that the permissions: publish_checkins or publish_actions is required. To remove this error, simply add these permissions while authenticating the user. See how it is done: Android login- permissions
But, the thing is- the API checkins have been deprecated. Check here it says-
Checkins have been deprecated in favor of attaching place information to posts, or tagging places in Open Graph stories. Please check the related guides for information.
So your code shall not work anyway. You'll now have to use Open Graph Actions for the same. There's step wise tutorial for the same in the documentation, just go through it and implement. Good luck.
I made android application that connects to remote server and send some data.
Remote server is Windows application.
Connection method:
private void ConnectToMonitor() {
try {
s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
This works perfectly if server is online. Application is sending data and server is receiving. But if server is offline android app. is blocked. My question is how to handle this? How to continue with application and avoid error even the server is down?
Remember to call this outside the UIThread.
Follow this tutorial. In android all connections need to be managed outside the UIThread, in the tutorial I linked you will find easy ways to post your results back to the UI (handlers, asynctasks...)
Of course we don't know if the problem is about the thread with just the given code, but it is the most usual error.
First remember to set the socket timeout :
mSocket.setSoTimeout(timeout); //in milliseconds
You can however specify different timeout for connection and for all other I/O operations through the socket:
private void connectToMonitor() {
try {
socket = new Socket();
InetAddress[] iNetAddress = InetAddress.getAllByName(SERVER_ADDRESS);
SocketAddress address = new InetSocketAddress(iNetAddress[0], TCP_SERVER_PORT);
socket.setSoTimeout(10000); //timeout for all other I/O operations, 10s for example
socket.connect(address, 20000); //timeout for attempting connection, 20 s
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Second, in Android, you should perform any network I/O in separate threads!
As an example, using regular Java Threads :
String threadName = getClass().getName() + "::connect";
new Thread(new Runnable() {
#Override
public void run() {
connectToMonitor();
}
}, threadName).start();
You can set A timeout for the socket. Use Socket.setSoTimeout method
socket.setSoTimeout(timesinmilis);
by using this your socket will throw a socket timout exception. You can catch that and do what you want
I could build a sip profile using SipProfile.Builder class. I used following snippet of code to do it:
if (SipManager.isApiSupported(MyActivity.this)&& SipManager.isVoipSupported(MyActivity.this)) {
SipManager manager=SipManager.newInstance(this);
SipProfile.Builder builder;try {
builder = new Builder(userName,domainName);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} manager = SipManager.newInstance(MyActivity.this);
builder.setPassword(password);
profile = builder.build();
try {
manager.open(profile);
} catch (SipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
manager.register(profile, 30, MyActivity.this);
} catch (SipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(MyActivity.this, "created",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MyActivity.this, "Not Supported",
Toast.LENGTH_LONG).show();
}
It shows as a sip account in the settings/callsettings/Internet call settings.
Problem is i could not make it as primary account. How can i make it as primary account?
This information is account-based (primary email address for person, primary phone number, primary SIP account etc.) so it's stored in the phone's contacts-book.
It seems to me it's an option that is set per-data, per-account (for example: this type of data (email/phone/sip-addr) set it as primary).
You can set data as primary for a contact using IS_PRIMARY or IS_SUPER_PRIMARY
IS_PRIMARY: Whether this is the primary entry of its kind for the raw
contact it belongs to.
The data you should use it on is ContactsContract.CommonDataKinds.SipAddress. I hope that's useful as starting point.