""

Tag Archive: phonegap

Another post on phonegap and my not so called “adventure”. This time something interesting happened. I am working on phonegap project targeting both iphone and android devices. Its mostly been REST based. So i was testing on various android and iphone devices, things went well except few things like static footer and header not working from android 2.x and below phones but i’l come to this issue on my next post. I hadn’t tested on android 4.0 and above, so yeah when i ran the app on icecream sandwich(Sony Xperia U and Samsung galaxy s2) mighty exception punched me on my face. Here is the long excpetion trace

10-26 10:19:45.998: E/AndroidRuntime(23917): FATAL EXCEPTION: main
10-26 10:19:45.998: E/AndroidRuntime(23917): android.os.NetworkOnMainThreadException
10-26

10:19:45.998: E/AndroidRuntime(23917):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-26 10:19:45.998: E/AndroidRuntime

(23917):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at java.net.InetAddress.getAllByName

(InetAddress.java:220)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpConnection.(HttpConnection.java:71)
10-26 10:19:45.998:

E/AndroidRuntime(23917):  at libcore.net.http.HttpConnection.(HttpConnection.java:50)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpEngine.sendRequest

(HttpEngine.java:232)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpURLConnectionImpl.getResponse

(HttpURLConnectionImpl.java:273)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpURLConnectionImpl.getInputStream

(HttpURLConnectionImpl.java:168)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpsURLConnectionImpl.getInputStream

(HttpsURLConnectionImpl.java:270)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at com.facebook.android.Util.openUrl(Util.java:215)
10-26 10:19:45.998:
.
.

(FbDialog.java:146)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:234)
10-26

10:19:45.998: E/AndroidRuntime(23917):  at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:349)
10-26 10:19:45.998: E/AndroidRuntime(23917):  

at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at android.os.Looper.loop(Looper.java:137)
10-26

10:19:45.998: E/AndroidRuntime(23917):  at android.app.ActivityThread.main(ActivityThread.java:4507)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

java.lang.reflect.Method.invokeNative(Native Method)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at java.lang.reflect.Method.invoke(Method.java:511)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-26 10:19:45.998:

E/AndroidRuntime(23917):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

dalvik.system.NativeStart.main(Native Method)

So exception thrown was NetworkOnMainThreadException, so first thing, i checked out the documentation for the details. So according to reference:

“The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it’s heavily discouraged”.

So one thing became clear to me that phonegap-facebook plugin is performing networking operation for facebook on main thread rather than separate thread like asynctask. From API level 11, httpclient is not allowed to execute on main thread. So i found the way to remove this restriction by changing the thread policy. Here is the small snippet that goes on onCreate():

if (android.os.Build.VERSION.SDK_INT >= 11) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

Phonegap doing facebook

I am here again to write on few more pitfalls that i fell into while setting up facebook-phonegap plugin. Set up is explained in the github project site. Just read the steps carefully and follow them one by one, don’t skip steps just for safety. Steps are clear enough to have setup your facebook plugin with phonegap project on xcode. My story starts when i finished all steps and finished setting up my project. So after everything was covered i built my project and fell straight into issues (not many issues and this particular one wasn’t any big):

1) “SenTestingKit.h not found”, yeah so some files from facebook sdk were using SenTestingKit.h which wasn’t created on my project. So to solve this issue, i had to link SenTestingKit.framework from Build phases tab -> Link Binary with libraries to project target.

Now my above issue was resolved. But real pain came next and new issue arose:

2) Undefined symbols for architecture i386: “_OBJC_CLASS_$_SenTestCase”, this gave me pain in my wherever you can think of, for hours. To be honest i was totally blank on this and didn’t know what to do. So i searched forums for this. It seemed that similar kind of problem was faced by many people. I tried every available answers and fixes but none worked for me. I even created and setup project from scratch again just to make sure if i hadn’t skipped any steps. But again, (yeah there is always a but somewhere) nothing worked. So i dig more deeper into my source files of facebook sdk. It seemed all the source files inside test folder were using this “SenTestingKit”. So i simply deleted that test folder from my project navigator, and it really resolved this issue or i removed the source of issue. No more such errors and no more pain and i don’t know about any gain.

But i wasn’t still sure if it didn’t break up the project as you don’t simply delete files and folders from external libraries without knowing. So i tested my project and performed all facebook activities and it seemed to worked fine. I haven’t found any issue till now but lets see.