Android Open Source - post-to-friendfeed Entry Service






From Project

Back to project page post-to-friendfeed.

License

The source code is released under:

Copyright (c) 2010, Larry Myers All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: R...

If you think the Android project post-to-friendfeed 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.larrymyers.android.posttoff;
/*  w  w w .  j  a v a2  s.  c  o m*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;

import android.util.Log;

public class EntryService {
    private static final String HOST = "friendfeed-api.com";
    private static final String PATH = "/v2/entry";
    
    public static void send(Entry entry, Auth auth) throws Exception {
        HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
            public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
                AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
                CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
                        ClientContext.CREDS_PROVIDER);
                HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
                
                if (authState.getAuthScheme() == null) {
                    AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                    Credentials creds = credsProvider.getCredentials(authScope);
                    if (creds != null) {
                        authState.setAuthScheme(new BasicScheme());
                        authState.setCredentials(creds);
                    }
                }
            }    
        };
        
        Credentials defaultCreds = new UsernamePasswordCredentials(auth.getUsername(), auth.getRemoteKey());
        
        CredentialsProvider credProvider = new BasicCredentialsProvider();
        credProvider.setCredentials(new AuthScope(HOST, 80), defaultCreds);
        
        DefaultHttpClient client = new DefaultHttpClient();
        client.addRequestInterceptor(preemptiveAuth, 0);
        client.setCredentialsProvider(credProvider);
        
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("body", entry.getBody()));
        nvps.add(new BasicNameValuePair("comment", entry.getFirstComment()));
        
        HttpPost request = new HttpPost("http://" + HOST + PATH);
        
        request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
            
        if (entity != null) {
            InputStream is = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            
            String line = null;
            while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
            }
            
            is.close();
            
            Log.d("HttpResponse", sb.toString());
        }
    }
}




Java Source Code List

com.larrymyers.android.posttoff.AuthAdapter.java
com.larrymyers.android.posttoff.AuthDbAdapter.java
com.larrymyers.android.posttoff.AuthPrefAdapter.java
com.larrymyers.android.posttoff.AuthView.java
com.larrymyers.android.posttoff.Auth.java
com.larrymyers.android.posttoff.EntryAdapter.java
com.larrymyers.android.posttoff.EntryDbAdapter.java
com.larrymyers.android.posttoff.EntryService.java
com.larrymyers.android.posttoff.EntryView.java
com.larrymyers.android.posttoff.Entry.java
com.larrymyers.android.posttoff.OpenFF.java