Android Open Source - sharemore Url Deshortener






From Project

Back to project page sharemore.

License

The source code is released under:

GNU General Public License

If you think the Android project sharemore listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright (c) 2012 Moarub Oy.//from   w ww . j a v a2 s .c o m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Moarub Oy - initial API and implementation
 ******************************************************************************/
package com.moarub.util;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.params.HttpClientParams;

import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.Log;

public class UrlDeshortener extends AsyncTask<String, Void, DeshorteningResult> {
  private UrlDeshortenerListener fListener;
  private String fUrlTo;
  private int fRedirects;
  private boolean fAsync;

  public UrlDeshortener(UrlDeshortenerListener listener, int count) {
    fListener = listener;
    fRedirects = count;
  }

  @Override
  protected DeshorteningResult doInBackground(String... urls) {
    return doShortening(urls);
  }

  private DeshorteningResult doShortening(String... urls) {
    fAsync = true;
    Log.d("Deshortening", "URL " + urls[0] + "(" + fRedirects + ")");
    fUrlTo = urls[0];
    AndroidHttpClient httpClient = AndroidHttpClient
        .newInstance("Android ShareMore");

    HttpHead headReq = new HttpHead(fUrlTo);

    try {
      HttpResponse resp = httpClient.execute(headReq);
      String resValue = resp.getLastHeader("Location") != null ? resp
          .getLastHeader("Location").getValue() : null;
      int statusCode = resp.getStatusLine().getStatusCode();
      return new DeshorteningResult(resValue, statusCode);
    } catch (IOException e) {
      Log.d("Deshortening", e.getMessage());
      return null;
    } finally {
      httpClient.close();
    }
  }

  @Override
  protected void onPostExecute(DeshorteningResult result) {
    handleResponse(result);
  }

  private void handleResponse(DeshorteningResult result) {
    if (result != null && result.getResValue() != null && fRedirects < 20) {

      fListener.onURLDeshortened(result.getResValue(),
          result.getStatusCode(), fUrlTo);
      Log.d("Deshortening",
          result.getResValue() + " (" + result.getStatusCode() + ")");

      if (result.getStatusCode() >= 300 && result.getStatusCode() < 400) {
        UrlDeshortener followOn = new UrlDeshortener(fListener,
            fRedirects + 1);
        followOn.execute(result.getResValue());
      }
      return;
    } else if (result != null) {
      if (fUrlTo != null && fAsync) {
        extractTitle(fUrlTo);
      }
      return;
    }
    fListener.onURLDeshortened(null, 700, fUrlTo);
  }

  private void extractTitle(String url) {
    PageTitleGetter pgt = new PageTitleGetter(fListener);
    String[] params = {url,url};
    pgt.execute(params);
  }

}




Java Source Code List

com.moarub.db.ListItem.java
com.moarub.db.ListsDataSource.java
com.moarub.db.ListsDbHelper.java
com.moarub.kipptapi.ApiTokenListener.java
com.moarub.kipptapi.ClipCreatedListener.java
com.moarub.kipptapi.CreateClip.java
com.moarub.kipptapi.KipptAPIToken.java
com.moarub.kipptapi.KipptClipItem.java
com.moarub.kipptapi.ListsGetter.java
com.moarub.kipptapi.ListsListener.java
com.moarub.sharemore.GettingStartedActivity.java
com.moarub.sharemore.GettingStartedView.java
com.moarub.sharemore.LoginActivity.java
com.moarub.sharemore.QuickShareToKipptActivity.java
com.moarub.sharemore.SaveToKipptService.java
com.moarub.sharemore.ShareMorePreferenceFragment.java
com.moarub.sharemore.ShareMorePreferencesActivity.java
com.moarub.sharemore.ShareToKipptActivity.java
com.moarub.util.DeshorteningResult.java
com.moarub.util.PageTitleGetter.java
com.moarub.util.ShareMoreUtils.java
com.moarub.util.UrlDeshortenerListener.java
com.moarub.util.UrlDeshortener.java