2012-12-12 61 views
17

假設使用Tumblr API上傳圖片很簡單。事實並非如此。 (編輯它是現在,看到編輯2在此入口的一端)將圖片上傳到Android的tumblr API

我的應用程序應該上傳圖片到tumblr。我寧願從服務中做到這一點,但現在我使用一種活動,只要完成上傳就會自動關閉。在OnCreate()用戶進行身份驗證:

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))); 

} 

這將打開一個瀏覽器活動,用戶可以添加用戶名和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, "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(); 

大多數這兩個片段我from here和他們的工作很好的。

有了這些令牌,我現在可以嘗試把數據放在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"); 

     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(); 
} 

正如你可以在這裏看到http://www.tumblr.com/blog/snapnowandroid(至少在這段時間)文本「這僅僅是一個測試」張貼。

但是,當我嘗試發佈圖像時,會變得很奇怪。現在我已經檢查過了,顯然這是一個衆所周知的tumblr API問題,這個問題已經被過多討論過了,其中一些已經在其他編程語言中解決了(例如here),但我一直無法重複這些成功。

所述的方法(在下文中其全部)具有完全相同的結構,以上述方法(即作品),所述namevaluepairs中只是不同

的方法,被賦予一個位圖變量稱爲照片:

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))); 

結果是來自tumblr api的{"meta":{"status":400,"msg":"Bad Request"},"response":{"errors":["Error uploading photo."]}}

我試圖對​​中描述的圖片進行不同的編碼,但沒有任何更改。

//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"); 

     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(); 
} 

現在,雖然有幾件事情我不滿意(例如,這是使用一個活動,而不是服務的實現)這裏的大問題顯然是上傳圖像的問題。我不是第一個遇到這個問題的人,所以有人能夠在java中完成這個任務嗎?

編輯1

還未與手頭的問題取得任何進展,但創造了可能是誰有同樣的問題的人一個很好的解決方法。 Tumblr提供posting via mail,您可以編程android以在後臺發送電子郵件爲shown here。這很有效,但你需要讓用戶提供他們的郵件帳戶數據和Tumblr郵件地址發佈。

編輯2

年已經pased和使用電子郵件不再是簡單的方法來做到這一點。隨着jumblr終於有一個良好的Java API可以在Android上使用。 OAuth認證是沒有趣味的(它永遠不會),但一旦你通過這個,它的奇妙。

現在,技術上來說,如何進行身份驗證並不屬於這裏,但這是我的問題,所以我只是在這裏粘貼一些代碼,如果它沒有意思,就跳過它。

這使用了一個名爲罐子jumblr-0.0.10-JAR-與-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; 
} 


} 

的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 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(); 
     } 
    } 
} 
} 
+0

爲什麼你是不是像'type'和'caption'那樣在名稱 - 值對中對'data'輸入名稱進行編碼? – Madbreaks

+0

實際上,對輸入名稱進行編碼並不是必要的(正如從文章開頭的文本文章的工作代碼中可以看到的那樣),這只是我試圖使其起作用的一件事。應該在示例代碼中再次考慮到這一點。 –

+1

我注意到的第一件事是你使用'data'而不是'source',但是你發送一個字符串而不是一個數組。如果您只是上傳一張照片,請嘗試使用帶編碼字符串或數據的'source'作爲包含一個編碼字符串元素的數組。 – Ally

回答

3

你爲什麼不使用Jumblr Tumblr的官方Java客戶端。

問候。

+1

從提交歷史我收集,它不存在當我發佈的問題。現在看起來值得一看。謝謝。 –

+0

哎呀,也許我以爲你有一個助焊劑電容:D – eltabo

+0

我試過Jumblr,我無法讓圖像上傳工作... https://stackoverflow.com/questions/26271523/jumblr-post-photo-on- android – mythicalprogrammer

0

這個工作對我來說...

nameValuePairs.add(new BasicNameValuePair(URLEncoder 
       .encode("type", "UTF-8"), 
        URLEncoder.encode("photo", "UTF-8"))); 
Log.e("Tumblr", "Image shareing file path" + filePath); 
nameValuePairs.add(new BasicNameValuePair("caption", caption)); 
nameValuePairs.add(new BasicNameValuePair("source", filePath));` 

其中,filepath是HTTP URL。

+0

感謝您的回答。我已經提交了一個編輯來整理格式 - 當詢問或回答問題時,如果您使用四個空格縮進代碼而不是使用嚴重口音,因爲它們不尊重換行符,所以更加整潔。 – nurdglaw

+0

我得到了:{「meta」:{「status」:400,「msg」:「Bad Request」},「response」:{「errors」:[「Error uploadloading photo。」}} –

0

我已經完成了使用下面的方法。你可以試試這個。

//中的paramString =「TEXT要放在標題」

private void postPhotoTumblr(String uploadedImagePhotoUrl, String paramString) 
{ 
    CommonsHttpOAuthConsumer localCommonsHttpOAuthConsumer = getTumblrConsumer(); 
    String str1 = "logged in username"; 
    String encodedImage = uploadedImagePhotoUrl; 
    DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient(); 
    HttpPost localHttpPost = new HttpPost("http://api.tumblr.com/v2/blog/" + str1 + ".tumblr.com/post"); 
    try 
    { 

    ArrayList localArrayList = new ArrayList(); 
    localArrayList.add(new BasicNameValuePair("type", "photo")); 
    BasicNameValuePair localBasicNameValuePair = new BasicNameValuePair("caption", paramString); 
    localArrayList.add(localBasicNameValuePair); 
    localArrayList.add(new BasicNameValuePair("data",encodedImage)); 
    UrlEncodedFormEntity localUrlEncodedFormEntity = new UrlEncodedFormEntity(localArrayList); 
    localHttpPost.setEntity(localUrlEncodedFormEntity); 
    localCommonsHttpOAuthConsumer.sign(localHttpPost); 
    InputStream localInputStream = localDefaultHttpClient.execute(localHttpPost).getEntity().getContent(); 
    InputStreamReader localInputStreamReader = new InputStreamReader(localInputStream); 
    BufferedReader localBufferedReader = new BufferedReader(localInputStreamReader); 
    StringBuilder localStringBuilder = new StringBuilder(); 
    while (true) 
    { 
     String str2 = localBufferedReader.readLine(); 
     if (str2 == null) 
     { 
     Log.i("DATA post resp", localStringBuilder.toString()); 
     break; 
     } 
     localStringBuilder.append(str2); 
    } 
    } 
    catch (ClientProtocolException localClientProtocolException) 
    { 
    localClientProtocolException.printStackTrace(); 
    } 
    catch (IOException localIOException) 
    { 
    localIOException.printStackTrace(); 
    } 
    catch (OAuthMessageSignerException localOAuthMessageSignerException) 
    { 
    localOAuthMessageSignerException.printStackTrace(); 
    } 
    catch (OAuthExpectationFailedException localOAuthExpectationFailedException) 
    { 
    localOAuthExpectationFailedException.printStackTrace(); 
    } 
    catch (OAuthCommunicationException localOAuthCommunicationException) 
    { 
    localOAuthCommunicationException.printStackTrace(); 
    } 
} 

編輯:那麼先上傳圖片到網絡服務器獲取網址,並嘗試與上傳的URL或文件路徑來發布。它會正常工作確保... :)

+0

我得到{ meta「:{」status「:400,」msg「:」Bad Request「},」response「:{」errors「:[」Error uploadloading photo。「}} –

+0

我可以發短信一個圖像。將。 ü幫助我PLZ –

+0

@Akanksha嘗試使用編輯的Code.above它將工作 –

0

我必須使用多 公共類VideoUploader擴展的AsyncTask {

ProgressDialog progressDialog; 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     progressDialog = ProgressDialog.show(RecordingActivity.this, "", 
       "Uploading video.. "); 
     super.onPreExecute(); 
    } 

    @Override 
    protected JSONObject doInBackground(String... params) { 
     JSONObject jsonObject = null; 
     StringBuilder builder = new StringBuilder(); 
     try { 
      String url = UrlConst.VIDEO_URL; 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 

      FileBody filebodyVideo = new FileBody(new File(params[0])); 
      StringBody title = new StringBody("uploadedfile: " + params[0]); 
      StringBody description = new StringBody(
        "This is a video of the agent"); 
      // StringBody code = new StringBody(realtorCodeStr); 

      MultipartEntity reqEntity = new MultipartEntity(); 
      reqEntity.addPart("uploadedfile", filebodyVideo); 
      reqEntity.addPart("title", title); 
      reqEntity.addPart("description", description); 
      // reqEntity.adddPart("code", code); 
      httppost.setEntity(reqEntity); 

      // DEBUG 
      System.out.println("executing request " 
        + httppost.getRequestLine()); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity resEntity = response.getEntity(); 

      // DEBUG 
      StatusLine status = response.getStatusLine(); 
      int statusCode = status.getStatusCode(); 
      System.out.println(response.getStatusLine()); 
      if (resEntity != null) { 
       System.out.println(EntityUtils.toString(resEntity)); 
      } // end if 

      if (resEntity != null) { 
       resEntity.consumeContent(); 
      } // end if 
      if (statusCode == 200) { 
       InputStream content = resEntity.getContent(); 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(content)); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        builder.append(line); 
       } 
       jsonObject = new JSONObject(builder.toString()); 
       return jsonObject; 
      } else { 
       Log.e(LoginActivity.class.toString(), 
         "Failed to download file"); 
      } 
      httpclient.getConnectionManager().shutdown(); 

     } catch (Exception e) { 
      // TODO: handle exception 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(JSONObject result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     progressDialog.dismiss(); 
     if (result != null) { 

      try { 

       JSONObject jsonObject = result 
         .getJSONObject(ParsingTagConst.COMMANDRESULT); 
       String strSuccess = jsonObject 
         .getString(ParsingTagConst.SUCCESS); 
       String responseString = jsonObject 
         .getString(ParsingTagConst.RESPONSE_STRING); 
       Toast.makeText(RecordingActivity.this, "" + responseString, 
         Toast.LENGTH_LONG).show(); 
       if (strSuccess.equals("1")) { 
        // get here your response 
       } 

      } catch (Exception e) { 
       // TODO: handle exception 
      } 
     } 

    } 

} 



enter code here 
3

您可以輕鬆地做到這一點使用jumblr - 的tumblr Java客戶端

JumblrClient client = new JumblrClient(Constant.CONSUMER_KEY,Constant.CONSUMER_SECRET); 

client.setToken(preferences.getString("token",null), preferences.getString("token_secret", null)); 

PhotoPost pp = client.newPost(client.user().getBlogs().get(0).getName(),PhotoPost.class); 

pp.setCaption(caption); 
// pp.setLinkUrl(link); 
// pp.setSource(mImage); // String URL 
pp.setPhoto(new Photo(imgFile)); 
pp.save(); 
+1

謝謝,eltabo在另一個答案中提到了這一點。當我有這個問題jumblr不存在,現在它是最好的解決方案。 –

+0

已編輯我的答案,以反映這一點。 –