Back to project page ReadabilitySDK.
The source code is released under:
MIT License
If you think the Android project ReadabilitySDK 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 com.idemidov.readability.command; /*from ww w.j a v a 2 s.c om*/ import android.content.Context; import android.util.Log; import com.idemidov.readability.data.CommandResponse; import com.idemidov.readability.exception.AuthorizationException; import com.idemidov.readability.exception.BadRequestException; import com.idemidov.readability.exception.ForbiddenException; import com.idemidov.readability.exception.InternalErrorException; import com.idemidov.readability.exception.NoConnectionException; import com.idemidov.readability.exception.NotFoundException; import com.idemidov.readability.parser.Parser; import com.idemidov.readability.util.ErrorHandler; import com.idemidov.readability.util.InternetAccessUtil; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONException; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; import oauth.signpost.OAuthConsumer; import oauth.signpost.exception.OAuthCommunicationException; import oauth.signpost.exception.OAuthExpectationFailedException; import oauth.signpost.exception.OAuthMessageSignerException; /** * Created by Ilya on 25/02/14. */ @SuppressWarnings("serial") public abstract class BaseCommand<T> implements Command<T> { protected final static String CONTENT_TYPE_NAME = "Content-Type"; protected final static String CONTENT_TYPE_VALUE = "application/x-www-form-urlencoded"; protected String TAG = getClass().getSimpleName(); protected String ROOT_URL = "https://readability.com/api/rest/v1/"; private OAuthConsumer consumer; public BaseCommand(OAuthConsumer consumer) { this.consumer = consumer; } @Override public CommandResponse<T> execute(Context context) throws NoConnectionException, IOException, JSONException, OAuthCommunicationException, OAuthExpectationFailedException, OAuthMessageSignerException, URISyntaxException, BadRequestException, AuthorizationException, ForbiddenException, NotFoundException, InternalErrorException { if (!InternetAccessUtil.hasConnection(context)) { throw new NoConnectionException(); } Log.d(TAG, "=== REQUEST === : ".concat(getUrl())); HttpClient httpClient = new DefaultHttpClient(); HttpUriRequest httpRequest = getHttpUriRequest(); consumer.sign(httpRequest); HttpResponse response = httpClient.execute(httpRequest); Log.d(TAG, "=== STATUS CODE === : " + response.getStatusLine().getStatusCode()); ErrorHandler.handleHttpCode(response.getStatusLine().getStatusCode()); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream inStream = entity.getContent(); String result = convertStreamToString(inStream); Log.d(TAG, "=== RESULT === : ".concat(result)); inStream.close(); return getParser() == null ? null : getParser().parse(result); } return null; } abstract Parser<T> getParser(); abstract String getUrl(); abstract HttpUriRequest getHttpUriRequest() throws UnsupportedEncodingException, JSONException, OAuthCommunicationException, OAuthExpectationFailedException, OAuthMessageSignerException, URISyntaxException; private static String convertStreamToString(InputStream is) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); return sb.toString(); } }