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

}

} i am using foursqure functionality in my application in which user share any data so user can share on foursqure..,the problem is when i am using "StrictMode "functionality in my oncreate.,its not giving me error.,but when i am not using its giving me networkon minthread exception i am getting this exception in foursqure.when dialog is loading??what i do plese help me thankyou...:) here is my logcatlogcat

rajshree
  • 790
  • 5
  • 19
  • 1
    you should use AsyncTask for that... – Piyush Jan 28 '14 at 13:55
  • @laalto sir.., when i am using strictmode option,i am not getting any error my,response is coming,but i am littile bit confuse where to use asyncronous task in application?can you help me? – rajshree Jan 28 '14 at 13:55
  • but it is not good for all you have to use AsyncTask . – Piyush Jan 28 '14 at 13:56
  • perhaps on this line Util.openUrl(url, httpMethod, params); – A.S. Jan 28 '14 at 13:58
  • @A.S.sir i try thnku..:) – rajshree Jan 28 '14 at 14:01
  • 1
    If you are using StrictMode then you're just stopping the exception from being thrown. There's a reason why that exception gets thrown and that's because it is a bad idea to retrieve data from the internet on the UI thread. If you have a slow connection, which is a problem in many parts of the world, you'll end up with a hanging application which eventually gives a 'Force Close' message. That is NOT professional programming – Piyush Jan 28 '14 at 14:02
  • @PiyushGupta thnku i try...:) – rajshree Jan 28 '14 at 14:03
  • @PiyushGupta sir i am confuse how i implement this,can you give me any example..,please...sir..:) – rajshree Jan 28 '14 at 14:24
  • @A.S. sir how i implemet that.,i tried but its giving error.,how i us ethat ,please help me – rajshree Jan 28 '14 at 14:28
  • Post your class in which you are perform network operation. – Piyush Jan 29 '14 at 04:53
  • @PiyushGupta goodmorning sir i have posted that class in my post – rajshree Jan 29 '14 at 05:07
  • @PiyushGupta sir...,that clas sis defined in my question... – rajshree Jan 29 '14 at 05:16
  • StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); in your class, – Digvesh Patel Jan 29 '14 at 14:10

2 Answers2

2

Networking operations tend to take some time. That's why on Android, it's forbidden to perform Networking Tasks on the UI (foreground) thread. The System throws a NetworkingOnMainThreadException if you try it.

You have to move your Networking code to a Background thread. The easiest way to do that is by using an AsyncTask.

FD_
  • 12,947
  • 4
  • 35
  • 62
-1

Implement AsyncTask, read here

XomeThing
  • 1
  • 1