Back to project page SyncFrameworkAndroid.
The source code is released under:
Apache License
If you think the Android project SyncFrameworkAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package pl.selvin.android.listsyncsample.authenticator; /* w ww .j ava 2 s . co m*/ import android.content.Context; import android.os.Handler; import android.util.Log; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import pl.selvin.android.listsyncsample.Constants; public class Common { private static String mAuthtoken; public static void sendResult(final Boolean result, final Handler handler, final Context context) { if (handler == null || context == null) { return; } handler.post(new Runnable() { public void run() { ((IAuthenticationResult) context) .onAuthenticationResult(result); } }); } public static boolean authenticate(String username, Handler handler, final Context context) { HttpRequestBase request = new HttpGet(Constants.SERVICE_URI + "/Login.ashx?username=" + username); request.setHeader("Accept", "application/json"); request.setHeader("Content-type", "application/json; charset=utf-8"); DefaultHttpClient httpClient = new DefaultHttpClient(); try { HttpResponse response = httpClient.execute(request); String bufstring = EntityUtils.toString(response.getEntity(), "UTF-8"); Log.d("ListSync", bufstring); mAuthtoken = bufstring; sendResult(true, handler, context); return true; } catch (Exception e) { Log.e("Auth", e.getLocalizedMessage()); } sendResult(false, handler, context); return false; } public static String getAuthtoken() { return mAuthtoken; } public static Thread performOnBackgroundThread(final Runnable runnable) { final Thread t = new Thread() { @Override public void run() { runnable.run(); } }; t.start(); return t; } public static Thread attemptAuth(final String username, final Handler handler, final Context context) { final Runnable runnable = new Runnable() { public void run() { authenticate(username, handler, context); } }; return performOnBackgroundThread(runnable); } public interface IAuthenticationResult { void onAuthenticationResult(boolean result); } }