""

Tag Archive: html

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);
}

RTL support on android

Hola everyone,
Long time no see 😀 I was making an app based on hebrew language. Its true that hebrew is not fully supported on all android phones. On some device it may work RTL and on some hebrew won’t be supported at all. Anyway here i am again to share something that i learned. On devices that supported hebrew(rendering) had a minor issue, lets say there is a number 52 on a hebrew sentence and when u try to display this sentence then 52 number is displayed reverse i.e 25. Now just imagine how messed up will whole sentence be because of this reverse number. Thus i found loophole in new android RTL support. So i had to do something about it as this was full hebrew language based app. I was using textview to display this hebrew sentence and this sentence was coming from xml web service. So i came up with my awesome solution. I chose to use webview to render/display hebrew sentence with html formatting. SO my code looked like this to prepare html:

public static String BuildHtml(String hebrewText)
{       
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append("");
sb.append("");
sb.append("");
sb.append(hebrewText.trim());
sb.append("");
sb.append("");
	
return sb.toString();
}

Now i load my html on a webview by calling my method that returns my hebrew string encoded in html format.

webView.loadData(BuildHtml(myHebrewText), "text/html", "utf-8");

This code worked flawlessly. The number 56 was getting displayed as 56 in a hebrew sentence.
I hope this helps those who faced simillar problem.

Gracias.