从Android上传图片到tumblr API [英] Uploading Images to tumblr API from Android

查看:115
本文介绍了从Android上传图片到tumblr API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设使用 Tumblr API 上传图片很容易。不是( EDIT 现在,请参阅本条结尾处的编辑2



我的应用程序应该将图片上传到 tumblr 。我更喜欢这样做,但是现在我使用一个活动,一旦完成上传就会关闭它。在 OnCreate()中,用户进行身份验证:

  consumer = new CommonsHttpOAuthConsumer (CONSUMER_KEY,CONSUMER_SECRET); 

//默认使用此签名
// consumer.setMessageSigner(new HmacSha1MessageSigner());

provider = new CommonsHttpOAuthProvider(REQUEST_TOKEN_URL,ACCESS_TOKEN_URL,AUTH_URL);

String authUrl;
try
{
authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URL);
Log.d(TAG,Auth url:+ authUrl);

startActivity(new Intent(android.intent.action.VIEW,Uri.parse(authUrl)));

}

这将打开浏览器活动,用户可以在其中添加用户名和passoword然后应用程序返回到活动(这也是为什么我必须使用活动,我不知道如何从一个服务这样做)



返回从浏览器中提取数据:

  Uri uri = context.getIntent()。getData(); 
if(uri!= null&& uri.toString()。startsWith(CALLBACK_URL))
{
Log.d(TAG,uri!= null);
String verifier = uri.getQueryParameter(oauth_verifier);
Log.d(TAG,验证者+验证者);
try
{
provider.setOAuth10a(true);
provider.retrieveAccessToken(consumer,verifier);
Log.d(TAG,try);
}
catch(异常e)
{
Log.e(TAG,e.toString());
e.printStackTrace();
}
OAUTH_TOKEN = consumer.getToken();
OAUTH_SECRET = consumer.getTokenSecret();

我获得的这两个片段中的大部分都是从这里,他们工作得很好。



使用这些令牌,我现在可以尝试将数据放在tumblr。当我尝试添加文本这可以很好的使用这种方法:

  private void createText()
{
if(!OAUTH_TOKEN.equals())
{

HttpContext context = new BasicHttpContext();
HttpPost request = new HttpPost(http://api.tumblr.com/v2/blog/+ blogname +.tumblr.com / post);

列表< NameValuePair> nameValuePairs = new ArrayList< NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair(type,text));
nameValuePairs.add(new BasicNameValuePair(body,这只是一个测试));

try
{
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
catch(UnsupportedEncodingException e1)
{
Log.e(TAG,e1.toString());
e1.printStackTrace();
}

if(consumer == null)
{
consumer = new CommonsHttpOAuthConsumer(OAuthConstants.TUMBR_CONSUMERKEY,OAuthConstants.TUMBR_SECRETKEY);
}
if(OAUTH_TOKEN == null || OAUTH_SECRET == null)
{
Log.e(TAG,Not logged in error);
}
consumer.setTokenWithSecret(OAUTH_TOKEN,OAUTH_SECRET);

try
{
consumer.sign(request);
}
catch(OAuthMessageSignerException e)
{

}
catch(OAuthExpectationFailedException e)
{
}
catch(OAuthCommunicationException e)
{
}
HttpClient client = new DefaultHttpClient();
//最后执行此请求
try
{
HttpResponse response = client.execute(request,context);
HttpEntity responseEntity = response.getEntity();
if(responseEntity!= null)
{
Log.d(TAG,responseEntety!= null);
try
{
Log.d(TAG,EntityUtils.toString(responseEntity));
}
catch(ParseException e)
{
e.printStackTrace();
Log.e(TAG,e.toString());
}
catch(IOException e)
{
e.printStackTrace();
Log.e(TAG,e.toString());
} //当我尝试上传照片时,给我{meta:{status:401,msg:未授权},response:[]}
}
else
{
Log.d(TAG,responseEntety == null);
}
}
catch(ClientProtocolException e)
{
// TODO自动生成的catch块
e.printStackTrace();
}
catch(IOException e)
{
// TODO自动生成的catch块
e.printStackTrace();
}
}
PostToTumblr.this.finish();
}

正如你可以在这里看到的那样 http://www.tumblr.com/blog/snapnowandroid (至少在这个时候)文本这只是一个测试是发布。



但是,当我尝试发布图像时,它变得奇怪。现在我检查了一下,显然这是一个tumblr API的一个众所周知的问题,它已经被过分讨论了这里,有些已经用其他编程语言解决了(例如这里),但我一直无法重复这些成功。



方法下面的全部)具有与上述方法(即工作)完全相同的结构,nameValuePairs不同



该方法给出了一个名为photo的Bitmap变量: / p>

  private void uploadToTumblr(Bitmap photo)

此位图转换为数组:

  ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
photo.compress(Bitmap.CompressFormat.PNG,100,stream);
byte [] bytes = stream.toByteArray();

nameValuePairs填充如下:

  nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode(type,enc),URLEncoder.encode(photo,enc))); 
nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode(caption,enc),URLEncoder.encode(text,enc)));
nameValuePairs.add(new BasicNameValuePair(data,Base64.encodeToString(bytes,Base64.URL_SAFE)));

结果是一个 {meta:{status:400 ,msg:错误请求},响应:{错误:[上传照片时出错。]}} 从tumblr api。



我尝试编码图片,如这篇文章,但没有任何变化。

  // http:// www。 coderanch.com/t/526487/java/java/Java-Byte-Hex-String 
final char [] hexArray = {'0','1','2','3','4' '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char [] hexChars = new char [bytes.length * 3];
int v;
for(int j = 0; j< bytes.length; j ++)
{
v = bytes [j]& 0xFF的;
hexChars [j * 3] ='%';
hexChars [j * 3 + 1] = hexArray [v>>>> 4];
hexChars [j * 3 + 2] = hexArray [v&为0x0F];
}
String s = new String(hexChars);
s = URLEncoder.encode(s,enc);
nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode(data,enc),s));

这里是整个方法(没有十六进制编码):

  private void uploadToTumblr(Bitmap photo)
{
if(!OAUTH_TOKEN.equals())
{

ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG,100,stream);
byte [] bytes = stream.toByteArray();

String text =SNAP;


HttpContext context = new BasicHttpContext();
HttpPost request = new HttpPost(http://api.tumblr.com/v2/blog/+ blogname +.tumblr.com / post);

列表< NameValuePair> nameValuePairs = new ArrayList< NameValuePair>(2);
String enc =UTF-8;

尝试
{
nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode(type,enc),URLEncoder.encode(photo,enc)));
nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode(caption,enc),URLEncoder.encode(text,enc)));
nameValuePairs.add(new BasicNameValuePair(data,Base64.encodeToString(bytes,Base64.URL_SAFE)));
}
catch(UnsupportedEncodingException e2)
{
Log.e(TAG,e2.toString());
e2.printStackTrace();
}
try
{
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
catch(UnsupportedEncodingException e1)
{
Log.e(TAG,e1.toString());
e1.printStackTrace();
}

if(consumer == null)
{
consumer = new CommonsHttpOAuthConsumer(OAuthConstants.TUMBR_CONSUMERKEY,OAuthConstants.TUMBR_SECRETKEY);
}
if(OAUTH_TOKEN == null || OAUTH_SECRET == null)
{
// throw new LoginErrorException(LoginErrorException.NOT_LOGGED_IN);
Log.e(TAG,未登录错误);
}
consumer.setTokenWithSecret(OAUTH_TOKEN,OAUTH_SECRET);

try
{
consumer.sign(request);
}
catch(OAuthMessageSignerException e)
{

}
catch(OAuthExpectationFailedException e)
{


catch(OAuthCommunicationException e)
{
}

HttpClient client = new DefaultHttpClient();

//最后执行此请求
try
{
HttpResponse response = client.execute(request,context);
HttpEntity responseEntity = response.getEntity();
if(responseEntity!= null)
{
Log.d(TAG,responseEntety!= null);
try
{
Log.d(TAG,EntityUtils.toString(responseEntity));
}
catch(ParseException e)
{
e.printStackTrace();
Log.e(TAG,e.toString());
}
catch(IOException e)
{
e.printStackTrace();
Log.e(TAG,e.toString());
}
}
else
{
Log.d(TAG,responseEntety == null);
}
}
catch(ClientProtocolException e)
{
// TODO自动生成的catch块
e.printStackTrace();
}
catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}


}

else
{
Log.d(TAG,upload imposble ... Toklen not组);
}
PostToTumblr.this.finish();
}

现在,有几件事我不满意(例如这个是使用活动而不是服务完成的)这里的大问题显然是上传图像的问题。我绝对不是第一个有这个问题,所以有人能够在java中完成这个吗?



编辑1



在手边的问题上没有取得任何进展,创建了一个解决方案,对于具有相同问题的人来说可能是很好的。 Tumblr提供通过邮件发布,您可以编程android在后台发送电子邮件<这里显示的href =http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android =nofollow noreferrer>。这样做的效果非常好,但您需要要求用户提供邮件帐户数据和Tumblr-mail地址才能发布。



编辑2 / p>

已经有几年了,使用电子邮件已经不再是简单的方法了。通过 jumblr ,终于有一个很好的Java API,可以在Android上运行。 OAuth认证是没有趣味的(从来不是这样),但一旦你过去了,它的梦幻般的。



现在,技术上,如何做认证的问题不属于这是我这个太长的问题了,所以我只是在这里粘贴一些代码,如果你没有兴趣,可以跳过它。



这使用一个名为 jumblr-0.0.10-jar-with-dependencies.jar

  import android.app.Activity; 
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.tumblr.jumblr.JumblrClient;
import com.tumblr.jumblr.request.RequestBuilder;
import com.tumblr.jumblr.types.Blog;
import com.tumblr.jumblr.types.User;

import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.TumblrApi;
import org.scribe.model.Token;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;

import java.io.File;


public class Tumblr
{
private static final String PROTECTED_RESOURCE_URL =http://api.tumblr.com/v2/user/info;

静态OAuthService服务;
static令牌requestToken = null;


public static void share(final Activity ctx,File file)
{
Thread tt = new Thread(new Runnable()
{
@Override
public void run()
{
JumblrClient client = new JumblrClient(Tumblr_Constants.CONSUMER_KEY,Tumblr_Constants.CONSUMER_SECRET);
RequestBuilder requestBuilder = client.getRequestBuilder();
requestBuilder.setConsumer(Tumblr_Constants.CONSUMER_KEY,Tumblr_Constants.CONSUMER_SECRET);
SharedPreferences settings = ctx.getSharedPreferences(TumblrData,0);
String oauthToken = settings.getString(OauthToken );
String oauthTokenSecret = settings.getString(OauthSecret,);
if(oauthToken.equals()|| oauthTokenSecret.equals())
{
authenticate(ctx);
while(WebViewFragment.verifier.equals())
{
尝试{
Thread.sleep(100);
} catch(InterruptedException e){
e.printStackTrace();
}
}
String v = WebViewFragment.verifier;
令牌accessToken = authenticatefurther(v);
SharedPreferences.Editor edit = settings.edit();
edit.putString(OauthToken,accessToken.getToken());
edit.putString(OauthSecret,accessToken.getSecret());
edit.commit();
oauthToken = settings.getString(OauthToken,);
oauthTokenSecret = settings.getString(OauthSecret,);
}
if(!oauthToken.equals()&!oauthTokenSecret.equals())
{
client.setToken(oauthToken,oauthTokenSecret);

用户user = client.user();
System.out.println(user.getName());

(博客博客:user.getBlogs()){
Log.d(TUMBLR,blog.getTitle());
}
}
}

});
tt.start();

}

private static void authenticate(Context ctx){
service = new ServiceBuilder()
.provider(TumblrApi.class)
.apiKey(Tumblr_Constants.CONSUMER_KEY)
.apiSecret(Tumblr_Constants.CONSUMER_SECRET)
.callback(snapnao://snapnao.de/ok)// OOB被禁止。我们需要一个网址,更好的是在tumblr网站!
.build();


Log.d(TUMBLR,=== Tumblr的OAuth工作流===);
System.out.println();

//获取请求令牌
Log.d(TUMBLR,获取请求令牌...);
requestToken = service.getRequestToken();
Log.d(TUMBLR,获得请求令牌!);
Log.d(TUMBLR,);

Log.d(TUMBLR,现在去授权Scribe这里:);
Log.d(TUMBLR,service.getAuthorizationUrl(requestToken));

String url = service.getAuthorizationUrl(requestToken);


意图i =新意图(ctx,WebViewFragment.class);
i.putExtra(url,url);
ctx.startActivity(i);


}

私有静态令牌authenticatefurther(String v)
{
令牌accessToken = null;
Log.d(TUMBLR,粘贴验证者);
Log.d(TUMBLR,>>);

验证器验证器=新Verifier(v);
Log.d(TUMBLR,);

//交易访问令牌的请求令牌和Verfier
Log.d(TUMBLR,交易访问令牌的请求令牌);
accessToken = service.getAccessToken(requestToken,
verifier);
Log.d(TUMBLR,获取访问令牌!);
Log.d(TUMBLR,(如果你好奇,它看起来像这样:+ accessToken +));

Log.d(TUMBLR,);

return accessToken;
}


}

WebViewFragement看起来像这样:

  import android.app.Activity; 
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class WebViewFragment extends Activity
{
public static String verifier =;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.webviewfragment);

String url = getIntent()。getStringExtra(url);
Log.d(TUMBLR,webview->+ url);
WebView view =(WebView)findViewById(R.id.webView);
view.setWebViewClient(
new SSLTolerentWebViewClient()
);
view.getSettings()。setJavaScriptEnabled(true);
view.loadUrl(url);
}

// SSL错误容忍Web View Client
私有类SSLTolerentWebViewClient扩展WebViewClient {

@Override
public void onReceivedSslError(WebView view,SslErrorHandler handler,SslError error){
handler.proceed(); //忽略SSL证书错误
}

@Override
public void onPageStarted(WebView视图,String url,Bitmap favicon){
super.onPageStarted(view,url ,favicon);
Log.d(TUMBLR,++++++ url);
if(url.contains(oauth_verifier =))
{
String [] x = url.split(oauth_verifier =);
verifier = x [1] .replace(#_ = _,);
WebViewFragment.this.finish();
}
}
}
}


解决方案

为什么不使用Tumblr的官方Java客户端的 Jumblr



问候。


One assumed using the Tumblr API to upload images would be easy. It isn't. (EDIT It is now, see Edit 2 at the end of this entry)

My app is supposed to upload an image to tumblr. I would prefer doing that from a service but for now I use an activity that closes itself as soon as its done uploading. In OnCreate() the user is authenticated:

consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);

// It uses this signature by default
// consumer.setMessageSigner(new HmacSha1MessageSigner());

provider = new CommonsHttpOAuthProvider(REQUEST_TOKEN_URL,ACCESS_TOKEN_URL,AUTH_URL);

String authUrl;
try 
{
authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
Log.d(TAG, "Auth url:" + authUrl);

startActivity(new Intent("android.intent.action.VIEW", Uri.parse(authUrl)));

} 

This opens a browser activity where the user can add username and passoword and then the app returns to the activity (this is also why I have to use an activity, I don't know how to do this from a service)

Returning from the browser the data is extracted:

Uri uri = context.getIntent().getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) 
{  
    Log.d(TAG, "uri!=null");
    String verifier = uri.getQueryParameter("oauth_verifier");  
    Log.d(TAG, "verifier"+verifier);
    try 
    {
        provider.setOAuth10a(true);
        provider.retrieveAccessToken(consumer, verifier);
        Log.d(TAG, "try");
    } 
    catch (Exception e) 
    {
        Log.e(TAG, e.toString());
        e.printStackTrace();
    } 
OAUTH_TOKEN = consumer.getToken();
OAUTH_SECRET = consumer.getTokenSecret();

Most of these two snippets I got from here and they work well.

With these tokens I can now try putting data on tumblr. When I try to add Text this works fine using this method:

private void createText()
{
    if(!OAUTH_TOKEN.equals(""))
    {

        HttpContext context = new BasicHttpContext();
        HttpPost request = new HttpPost("http://api.tumblr.com/v2/blog/" + blogname + ".tumblr.com/post");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("type", "text")); 
        nameValuePairs.add(new BasicNameValuePair("body", "this is just a test"));  

        try 
        {
        request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        } 
        catch (UnsupportedEncodingException e1) 
        {
            Log.e(TAG, e1.toString());
            e1.printStackTrace();
        }

        if (consumer == null)
        {
            consumer = new CommonsHttpOAuthConsumer(OAuthConstants.TUMBR_CONSUMERKEY, OAuthConstants.TUMBR_SECRETKEY);
        }
        if (OAUTH_TOKEN == null || OAUTH_SECRET == null)
        {
            Log.e(TAG, "Not logged in error");
        }
        consumer.setTokenWithSecret(OAUTH_TOKEN, OAUTH_SECRET);

        try 
        {
            consumer.sign(request);
        } 
        catch (OAuthMessageSignerException e) 
        {

        } 
        catch (OAuthExpectationFailedException e) 
        {
        } 
        catch (OAuthCommunicationException e) 
        {
        }
        HttpClient client = new DefaultHttpClient();
        //finally execute this request
        try 
        {
            HttpResponse response = client.execute(request, context);
            HttpEntity responseEntity = response.getEntity(); 
            if (responseEntity != null) 
            { 
                Log.d(TAG, "responseEntety!=null");
                try 
                {
                    Log.d(TAG, EntityUtils.toString(responseEntity));
                } 
                catch (ParseException e) 
                {
                    e.printStackTrace();
                    Log.e(TAG, e.toString());
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                    Log.e(TAG, e.toString());
                } // gives me {"meta":{"status":401,"msg":"Not Authorized"},"response":[]} when I try to upload a photo
            }
            else
            {
                Log.d(TAG, "responseEntety==null");
            }
        } 
        catch (ClientProtocolException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    PostToTumblr.this.finish();
}

As you can see here http://www.tumblr.com/blog/snapnowandroid (at least as of this time) the text "this is just a test" is posted.

However, when I try to post images, it gets strange. Now I have checked around and apparently this is a well known issue with the tumblr API, which has excessively been discussed here and some have solved it in other programming languages (for example here) but I have been unable to repeat those successes.

The method (in its entirety below) has the exact same structure to the above method (that works), the nameValuePairs are just different

The method is given a Bitmap variable called photo:

    private void uploadToTumblr(Bitmap photo)

This bitmap is converted into an array:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bytes = stream.toByteArray();

The nameValuePairs are filled as follows:

nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode("type", enc), URLEncoder.encode("photo", enc)));
nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode("caption", enc), URLEncoder.encode(text, enc))); 
nameValuePairs.add(new BasicNameValuePair("data", Base64.encodeToString(bytes, Base64.URL_SAFE))); 

The result is a {"meta":{"status":400,"msg":"Bad Request"},"response":{"errors":["Error uploading photo."]}} from the tumblr api.

I have tries encoding the picture differently as discribed in this article but without any changes.

//http://www.coderanch.com/t/526487/java/java/Java-Byte-Hex-String
final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char[] hexChars = new char[bytes.length * 3];
int v;
for ( int j = 0; j < bytes.length; j++ ) 
{
    v = bytes[j] & 0xFF;
    hexChars[j * 3] = '%';
    hexChars[j * 3 + 1] = hexArray[v >>> 4];
    hexChars[j * 3 + 2] = hexArray[v & 0x0F];
}
String s = new String(hexChars);                
s = URLEncoder.encode(s, enc);
nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode("data", enc), s)); 

Here the entire method (without the hex encoding):

private void uploadToTumblr(Bitmap photo)
{
    if(!OAUTH_TOKEN.equals(""))
    {

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] bytes = stream.toByteArray();

        String text ="SNAP";


        HttpContext context = new BasicHttpContext();
        HttpPost request = new HttpPost("http://api.tumblr.com/v2/blog/" + blogname + ".tumblr.com/post");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
        String enc = "UTF-8"; 

        try 
        {
            nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode("type", enc), URLEncoder.encode("photo", enc)));
            nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode("caption", enc), URLEncoder.encode(text, enc))); 
            nameValuePairs.add(new BasicNameValuePair("data", Base64.encodeToString(bytes, Base64.URL_SAFE))); 
        } 
        catch (UnsupportedEncodingException e2) 
        {
            Log.e(TAG, e2.toString());
            e2.printStackTrace();
        } 
        try 
        {
            request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        } 
        catch (UnsupportedEncodingException e1) 
        {
            Log.e(TAG, e1.toString());
            e1.printStackTrace();
        }

        if (consumer == null)
        {
            consumer = new CommonsHttpOAuthConsumer(OAuthConstants.TUMBR_CONSUMERKEY, OAuthConstants.TUMBR_SECRETKEY);
        }
        if (OAUTH_TOKEN == null || OAUTH_SECRET == null)
        {
            //throw new LoginErrorException(LoginErrorException.NOT_LOGGED_IN);
            Log.e(TAG, "Not logged in error");
        }
        consumer.setTokenWithSecret(OAUTH_TOKEN, OAUTH_SECRET);

            try 
            {
                consumer.sign(request);
            } 
            catch (OAuthMessageSignerException e) 
            {

            } 
            catch (OAuthExpectationFailedException e) 
            {

            } 
            catch (OAuthCommunicationException e) 
            {
            }

            HttpClient client = new DefaultHttpClient();

            //finally execute this request
            try 
            {
                HttpResponse response = client.execute(request, context);
                HttpEntity responseEntity = response.getEntity(); 
                if (responseEntity != null) 
                { 
                    Log.d(TAG, "responseEntety!=null");
                    try 
                    {
                        Log.d(TAG, EntityUtils.toString(responseEntity));
                    } 
                    catch (ParseException e) 
                    {
                        e.printStackTrace();
                        Log.e(TAG, e.toString());
                    } 
                    catch (IOException e) 
                    {
                        e.printStackTrace();
                        Log.e(TAG, e.toString());
                    } 
                }
                else
                {
                    Log.d(TAG, "responseEntety==null");
                }
            } 
            catch (ClientProtocolException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }

    else
    {
        Log.d(TAG, "upload imposble... Toklen not set");
    }
    PostToTumblr.this.finish();
}

Now, while there are several things I am unhappy with (for example that this is done using an activity instead of a service) the big issue here is clearly the problem of uploading images. I am by no means the first to have this problem, so has anyone been able to get this done in java?

Edit 1

Have not made any progress with the problem at hand but created a workaround that might be nice for people who have the same issue. Tumblr offers posting via mail and you can programm android to send emails in the background as shown here. This works very well but you need to ask users to provide their mail account data and the Tumblr-mail Adress to post.

Edit 2

Years have pased and using email is no longer the easy way to do it. With jumblr there is finally a good API for Java that will work on android. OAuth-Authentication is no fun (it never is) but once you get past this, its fantastic.

Now, technically the question of how to do the authentication does not belong here but It's my overly long question, so I'll just paste some code here and if it's not interesting to you just skip it.

This uses a jar called jumblr-0.0.10-jar-with-dependencies.jar

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.tumblr.jumblr.JumblrClient;
import com.tumblr.jumblr.request.RequestBuilder;
import com.tumblr.jumblr.types.Blog;
import com.tumblr.jumblr.types.User;

import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.TumblrApi;
import org.scribe.model.Token;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;

import java.io.File;


public class Tumblr
{
private static final String PROTECTED_RESOURCE_URL = "http://api.tumblr.com/v2/user/info";

static OAuthService service;
static Token requestToken=null;


public static void share(final Activity ctx, File file)
{
    Thread tt = new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            JumblrClient client = new JumblrClient(Tumblr_Constants.CONSUMER_KEY, Tumblr_Constants.CONSUMER_SECRET);
            RequestBuilder requestBuilder = client.getRequestBuilder();
            requestBuilder.setConsumer(Tumblr_Constants.CONSUMER_KEY, Tumblr_Constants.CONSUMER_SECRET);
            SharedPreferences settings = ctx.getSharedPreferences("TumblrData", 0);
            String oauthToken=settings.getString("OauthToken", "");
            String oauthTokenSecret=settings.getString("OauthSecret", "");
            if(oauthToken.equals("") || oauthTokenSecret.equals(""))
            {
                authenticate(ctx);
                while(WebViewFragment.verifier.equals(""))
                {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                String v = WebViewFragment.verifier;
                Token accessToken = authenticatefurther(v);
                SharedPreferences.Editor edit = settings.edit();
                edit.putString("OauthToken", accessToken.getToken());
                edit.putString("OauthSecret", accessToken.getSecret());
                edit.commit();
                oauthToken=settings.getString("OauthToken", "");
                oauthTokenSecret=settings.getString("OauthSecret", "");
            }
            if(!oauthToken.equals("") && !oauthTokenSecret.equals(""))
            {
                client.setToken(oauthToken, oauthTokenSecret);

                User user = client.user();
                System.out.println(user.getName());

                for (Blog blog : user.getBlogs()) {
                    Log.d("TUMBLR", blog.getTitle());
                }
            }
        }

    });
    tt.start();

}

private static void authenticate(Context ctx) {
    service = new ServiceBuilder()
            .provider( TumblrApi.class )
            .apiKey(Tumblr_Constants.CONSUMER_KEY)
            .apiSecret(Tumblr_Constants.CONSUMER_SECRET)
            .callback("snapnao://snapnao.de/ok") // OOB forbidden. We need an url and the better is on the tumblr website !
            .build();


    Log.d("TUMBLR", "=== Tumblr's OAuth Workflow ===" );
    System.out.println();

    // Obtain the Request Token
    Log.d("TUMBLR", "Fetching the Request Token...");
    requestToken = service.getRequestToken();
    Log.d("TUMBLR", "Got the Request Token!");
    Log.d("TUMBLR", "");

    Log.d("TUMBLR", "Now go and authorize Scribe here:" );
    Log.d("TUMBLR", service.getAuthorizationUrl( requestToken ) );

    String url = service.getAuthorizationUrl(requestToken);


    Intent i = new Intent(ctx, WebViewFragment.class);
    i.putExtra("url", url);
    ctx.startActivity(i);


}

private static Token authenticatefurther(String v)
{
    Token accessToken = null;
    Log.d("TUMBLR", "And paste the verifier here");
    Log.d("TUMBLR", ">>");

    Verifier verifier = new Verifier( v);
    Log.d("TUMBLR", "");

    // Trade the Request Token and Verfier for the Access Token
    Log.d("TUMBLR", "Trading the Request Token for an Access Token...");
    accessToken = service.getAccessToken( requestToken ,
            verifier );
    Log.d("TUMBLR", "Got the Access Token!");
    Log.d("TUMBLR", "(if your curious it looks like this: " + accessToken + " )");

    Log.d("TUMBLR", "");

    return accessToken;
}


}

The WebViewFragement looks like this:

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class WebViewFragment extends Activity 
{
public static String verifier="";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webviewfragment);

    String url = getIntent().getStringExtra("url");
    Log.d("TUMBLR", "webview-> "+url);
    WebView view = (WebView) findViewById(R.id.webView);
    view.setWebViewClient(
            new SSLTolerentWebViewClient()
    );
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl(url);
}

// SSL Error Tolerant Web View Client
private class SSLTolerentWebViewClient extends WebViewClient {

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed(); // Ignore SSL certificate errors
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        Log.d("TUMBLR", "+++++"+url);
        if(url.contains("oauth_verifier="))
        {
            String[] x = url.split("oauth_verifier=");
            verifier=x[1].replace("#_=_", "");
            WebViewFragment.this.finish();
        }
    }
}
}

解决方案

Why don't you use Jumblr the official Java client for Tumblr.

Regards.

这篇关于从Android上传图片到tumblr API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆