2014-01-28 17 views
0
public class Foursquare { 

private static final String LOGIN = "oauth"; 
public static final String API_END_POING_BASE_URL = "https://api.foursquare.com/v2/"; 
public static String REDIRECT_URI; 
public static final String API_URL = "https://foursquare.com/oauth2/"; 
//public static final String CANCEL_URI = ""; 
public static final String TOKEN = "access_token"; 
public static final String EXPIRES = "expires_in"; 
public static final String SINGLE_SIGN_ON_DISABLED = "service_disabled"; 
public static String AUTHENTICATE_URL = "https://foursquare.com/oauth2/authenticate";// + 

private String mClientId; 
private String mClientSecret; 
private String mAccessToken = null; 

private DialogListener mAuthDialogListener; 

public Foursquare(String clientId, String clientSecret, String redirectUrl) { 
    if (clientId == null || clientSecret == null) { 
     throw new IllegalArgumentException(
       "You must specify your application ID when instantiating " 
         + "a Foursquare object. See README for details."); 
    } 
    mClientId = clientId; 
    mClientSecret = clientSecret; 
    REDIRECT_URI = redirectUrl; 
} 

public void authorize(Activity activity, final DialogListener listener) { 
    mAuthDialogListener = listener; 
    startDialogAuth(activity); 
} 

public void startDialogAuth(Activity activity) { 
    CookieSyncManager.createInstance(activity); 
    Bundle params = new Bundle(); 
    dialog(activity, LOGIN, params, new DialogListener() { 

     public void onComplete(Bundle values) { 
      // ensure any cookies set by the dialog are saved 
      CookieSyncManager.getInstance().sync(); 
      String _token = values.getString(TOKEN); 
      setAccessToken(_token); 
      // setAccessExpiresIn(values.getString(EXPIRES)); 
      if (isSessionValid()) { 
       Log.d("Foursquare-authorize", 
         "Login Success! access_token=" + getAccessToken()); 
       mAuthDialogListener.onComplete(values); 
      } else { 
       mAuthDialogListener.onFoursquareError(new FoursquareError(
         "Failed to receive access token.")); 
      } 
     } 

     public void onError(DialogError error) { 
      Log.d("Foursquare-authorize", "Login failed: " + error); 
      mAuthDialogListener.onError(error); 
     } 

     public void onFoursquareError(FoursquareError error) { 
      Log.d("Foursquare-authorize", "Login failed: " + error); 
      mAuthDialogListener.onFoursquareError(error); 
     } 

     public void onCancel() { 
      Log.d("Foursquare-authorize", "Login canceled"); 
      mAuthDialogListener.onCancel(); 
     } 
    }); 
} 

public void dialog(Context context, String action, Bundle parameters, 
     final DialogListener listener) { 

    String endpoint = ""; 

    parameters.putString("client_id", mClientId); 
    parameters.putString("display", "touch"); 
    if (action.equals(LOGIN)) { 
     endpoint = AUTHENTICATE_URL; 
     parameters.putString("client_secret", mClientSecret); 
     parameters.putString("response_type", "token"); 
     parameters.putString("redirect_uri", REDIRECT_URI); 
    } 

    //  if (isSessionValid()) { 
    //   parameters.putString(TOKEN, getAccessToken()); 
    //  } 
    String url = endpoint + "?" + Util.encodeUrl(parameters); 
    if (context.checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) { 
     Util.showAlert(context, "Error", 
       "Application requires permission to access the Internet"); 
    } else { 
     new FoursquareDialog(context, url, listener).show(); 
    } 
} 

public boolean isSessionValid() { 
    if (getAccessToken() != null) { 
     return true; 
    } 
    return false; 
} 

public void setAccessToken(String token) { 
    mAccessToken = token; 
} 

public String getAccessToken() { 
    return mAccessToken; 
} 

public String request(String graphPath) throws MalformedURLException, 
IOException { 
    return request(graphPath, new Bundle(), "GET"); 
} 

public String request(String graphPath, Bundle parameters) 
     throws MalformedURLException, IOException { 
    return request(graphPath, parameters, "GET"); 
} 

public String request(String graphPath, Bundle params, String httpMethod) 
     throws FileNotFoundException, MalformedURLException, IOException { 
    params.putString("format", "json"); 
    if (isSessionValid()) { 
     params.putString("oauth_token", getAccessToken()); 
    } 
    String url = API_END_POING_BASE_URL + graphPath; 
    return Util.openUrl(url, httpMethod, params); 
} 

public static interface DialogListener { 

    /** 
    * Called when a dialog completes. 
    * 
    * Executed by the thread that initiated the dialog. 
    * 
    * @param values 
    *   Key-value string pairs extracted from the response. 
    */ 
    public void onComplete(Bundle values); 

    /** 
    * Called when a Foursquare responds to a dialog with an error. 
    * 
    * Executed by the thread that initiated the dialog. 
    * 
    */ 
    public void onFoursquareError(FoursquareError e); 

    /** 
    * Called when a dialog has an error. 
    * 
    * Executed by the thread that initiated the dialog. 
    * 
    */ 
    public void onError(DialogError e); 

    /** 
    * Called when a dialog is canceled by the user. 
    * 
    * Executed by the thread that initiated the dialog. 
    * 
    */ 
    public void onCancel(); 

} 

} 我使用foursqure功能在我的應用程序中,用戶共享任何數據,因此用戶可以在foursqure分享..,問題是當我在我的OnCreate使用「StrictMode」的功能,它不是給我錯誤。,但是當我不使用它給我的網絡minthread異常 我得到這個異常在foursqure.when對話框正在加載??我做什麼plese幫助我thankyou ... :) 這裏是我的logcat logcatandroid網絡上的mainthread execption?

+1

你應該使用的AsyncTask爲... – Piyush

+0

@laalto先生..,當我使用strictmode選項,我沒有得到任何錯誤我的迴應即將到來,但我littile有點混淆在應用程序中使用異步任務的哪裏?你能幫助我嗎? – rajshree

+0

但它不適合所有你必須使用AsyncTask。 – Piyush

回答

2

網絡操作往往需要一些時間。這就是爲什麼在Android上,禁止在UI(前臺)線程上執行聯網任務的原因。如果您嘗試,系統會拋出一個NetworkingOnMainThreadException

您必須將網絡代碼移動到後臺線程。要做到這一點,最簡單的方法是使用一個AsyncTask.

+0

ok先生我試試.. :) – rajshree