Android : Unblocking UI Thread while avoiding NetworkOnMainThread Error

on Sunday, March 22, 2015


I have a function where I have to get code from server and return its operation.


Here is the code



public String getResult
{
// Call to your backend

try {
String url = String.format(Constants.LOGIN_URL,
URLEncoder.encode(countryCode + phoneNumber, "UTF-8"),
URLEncoder.encode(smsCode, "UTF-8"),
URLEncoder.encode(Constants.WHERE_API_KEY, "UTF-8"));

while (new LoginToServer().execute(url).get());

}catch (Exception e)
{}

return result; // Result will be saved to class in onPostExecute()


My LoginToServer returns true if there was an error retrieving the result. Now, This will most obviously remove the Exception of NetworkOnMainThread, however it iwll block UI.


since the function is to be called by some sdks , it is imperative that i make httpconnection so that it completes then pass on the result.


How can I make UI To unblock while executing this code ?


Again i can't call entire function as aysnc because the function is to be called by most sdk functions internally and they will diffidently not call it in Async


0 comments:

Post a Comment