Android Open Source - dw2020 Px Api






From Project

Back to project page dw2020.

License

The source code is released under:

Apache License

If you think the Android project dw2020 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

package com.fivehundredpx.api;
/*from   w  ww.  ja v  a 2s .  co  m*/
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.util.Log;

import com.fivehundredpx.api.auth.AccessToken;

public class PxApi {
  private static final String TAG = "ApiHelper";

  private static String HOST = "https://api.500px.com/v1";

  private AccessToken accessToken;
  private String consumerKey;
  private String consumerSecret;

  public PxApi(AccessToken accessToken, String consumerKey,
      String consumerSecret) {
    super();
    this.accessToken = accessToken;
    this.consumerKey = consumerKey;
    this.consumerSecret = consumerSecret;
  }

  public PxApi(String consumerKey) {
    super();
    this.consumerKey = consumerKey;
  }

  public JSONObject get(String url) {
    if (null != accessToken) {
      HttpGet request = new HttpGet(HOST + url);
      return handleSigned(request);
    } else {
      final String finalUrl = String.format("%s%s&consumer_key=%s", HOST,
          url, this.consumerKey);
      return handle(new HttpGet(finalUrl));
    }
  }

  public JSONObject post(String url, List<? extends NameValuePair> params) {
    HttpPost request = new HttpPost(HOST + url);
    try {
            if (params != null) request.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
      Log.e(TAG, "Parameters in post are invalid", e);
    }
    return handleSigned(request);
  }

  private void signRequest(HttpUriRequest request)
      throws OAuthMessageSignerException,
      OAuthExpectationFailedException, OAuthCommunicationException {
    CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(
        consumerKey, consumerSecret);
    consumer.setTokenWithSecret(accessToken.getToken(),
        accessToken.getTokenSecret());
    consumer.sign(request);
  }

  private JSONObject handleSigned(HttpUriRequest request) {
    try {
      signRequest(request);
    } catch (Exception e) {
      Log.e(TAG, "Erro trying to sign the request.", e);
    }
    return handle(request);
  }

  private JSONObject handle(HttpUriRequest request) {
    try {
      DefaultHttpClient client = new DefaultHttpClient();

      HttpResponse response = client.execute(request);
      final int statusCode = response.getStatusLine().getStatusCode();

      if (statusCode != HttpStatus.SC_OK) {
        final String msg = String.format(
            "Error, statusCode not OK(%d). for url: %s",
            statusCode, request.getURI().toString());
        Log.e(TAG, msg);
        return null;
      }

      HttpEntity responseEntity = response.getEntity();
      InputStream inputStream = responseEntity.getContent();
      BufferedReader r = new BufferedReader(new InputStreamReader(
          inputStream));
      StringBuilder total = new StringBuilder();
      String line;
      while ((line = r.readLine()) != null) {
        total.append(line);
      }

      JSONObject json = new JSONObject(total.toString());
      return json;
    } catch (Exception e) {
      Log.e(TAG, "Error obtaining response from 500px api.", e);
    }
    return null;
  }

}




Java Source Code List

com.fivehundredpx.api.FiveHundredException.java
com.fivehundredpx.api.PxApi.java
com.fivehundredpx.api.auth.AccessToken.java
com.fivehundredpx.api.auth.FacebookAuthProvider.java
com.fivehundredpx.api.auth.HttpParameterUtil.java
com.fivehundredpx.api.auth.OAuthAuthorization.java
com.fivehundredpx.api.auth.OAuthConstants.java
com.fivehundredpx.api.auth.OAuthParameters.java
com.fivehundredpx.api.auth.OAuthProvider.java
com.fivehundredpx.api.auth.PlusAuthProvider.java
com.fivehundredpx.api.auth.TwitterAuthProvider.java
com.fivehundredpx.api.auth.XAuthProvider.java
com.fivehundredpx.api.tasks.UserDetailTask.java
com.fivehundredpx.api.tasks.XAuth500pxTask.java
net.darkwire.example.BaseActivity.java
net.darkwire.example.BaseSpiceActivity.java
net.darkwire.example.MainActivity.java
net.darkwire.example.ObservableApp.java
net.darkwire.example.builder.FiveHundredPxAccessToken.java
net.darkwire.example.exception.AuthenticationError.java
net.darkwire.example.model.FiveHundredPxConfiguration.java
net.darkwire.example.model.FiveHundredPxImageMetadata.java
net.darkwire.example.model.FiveHundredPxPhotoContainer.java
net.darkwire.example.model.FiveHundredPxPhoto.java
net.darkwire.example.service.CacheableSpiceRequest.java
net.darkwire.example.service.FiveHundredPxGsonSpiceService.java
net.darkwire.example.service.FiveHundredPxJacksonSpiceService.java
net.darkwire.example.service.FiveHundredPxRecentPhotosSpiceRequest.java
net.darkwire.example.service.FiveHundredPxSearchSpiceRequest.java
net.darkwire.example.service.FiveHundredPxSpiceRequest.java
net.darkwire.example.service.FiveHundredPx.java
net.darkwire.example.service.catalog.UrlCatalog.java
net.darkwire.example.service.client.FiveHundredPxClient.java
net.darkwire.example.widgets.adapter.NumericPaginationBaseAdapter.java
net.darkwire.example.widgets.adapter.NumericPagination.java
net.darkwire.example.widgets.adapter.PhotoAdapter.java
net.darkwire.example.widgets.listener.InfiniteScrollListener.java
net.darkwire.example.widgets.view.SquareImageView.java
se.akerfeldt.signpost.retrofit.HttpRequestAdapter.java
se.akerfeldt.signpost.retrofit.RetrofitHttpOAuthConsumer.java
se.akerfeldt.signpost.retrofit.SigningOkClient.java