2012-12-12 9 views
17

Założenie, że przy przesyłaniu zdjęć ma być Tumblr API, będzie łatwe. Nie jest. (EDIT jest teraz, patrz Edycja 2 na końcu tego wpisu)Przesyłanie obrazów do interfejsu API tumblr z systemu Android

Moja aplikacja ma przesłać obraz do tumblr. Wolałbym to zrobić z usługi, ale na razie korzystam z działania, które zamyka się zaraz po zakończeniu przesyłania. W OnCreate() uwierzytelnieniu użytkownika:

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

} 

To otwiera działalność przeglądarki, w którym użytkownik może dodać nazwę użytkownika i passoword a następnie powraca aplikacji do działania (jest to również, dlaczego muszę używać aktywność, I don” t wiedzieć, jak to zrobić z usługi)

Wracając z przeglądarki dane są wyodrębnione:

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

Większość z tych dwóch fragmentów dostałem from here i działają dobrze.

Dzięki tym tokenom mogę teraz próbować umieszczać dane na tumblr. Gdy próbuję dodać tekst to działa prawidłowo przy użyciu tej metody:

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

Jak widać tutaj http://www.tumblr.com/blog/snapnowandroid (przynajmniej w tej chwili) tekst „to tylko test” jest zamieszczona.

Jednak, gdy próbuję publikować obrazy, robi się dziwnie. Teraz sprawdziłem i najwyraźniej jest to znany problem z API tumblr, które zostało nadmiernie omówione here, a niektóre rozwiązały je w innych językach programowania (na przykład here), ale nie udało mi się powtórzyć tych sukcesów.

Metoda (w całości poniżej) ma dokładnie taką samą strukturę powyższej metody (które działa), to nameValuePairs to tylko inna

otrzymuje zmienną bitmapy nazwie zdjęcie Metoda:

private void uploadToTumblr(Bitmap photo) 

Ta mapa bitowa jest przekształcany w tablicy:

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

w nameValuePairs są napełniane następująco:

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

Wynikiem jest {"meta":{"status":400,"msg":"Bad Request"},"response":{"errors":["Error uploading photo."]}} z api interfejsu tumblr.

Próbuję kodowania obrazu inaczej, jak opisano w this article, ale bez żadnych zmian.

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

Oto cała metoda (bez kodowania hex):

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

Teraz, gdy istnieje kilka rzeczy, nie jestem zadowolony z (na przykład, że odbywa się to za pomocą aktywności zamiast usługi) istotną kwestią jest oczywiście problem przesyłania zdjęć. Nie jestem pierwszym, który ma ten problem, więc czy ktoś był w stanie to zrobić w Javie?

Edycja 1

nie dokonano żadnego postępu z problemem pod ręką, ale stworzył obejście, które mogą być miły dla ludzi, którzy mają ten sam problem. Tumblr oferuje posting via mail i można zaprogramować Androida do wysyłania wiadomości e-mail w tle jako shown here. Działa to bardzo dobrze, ale musisz poprosić użytkowników o podanie danych swojego konta pocztowego i adresu Tumblr-mail, aby opublikować.

Edycja 2

Lata pased i za pomocą poczty elektronicznej nie jest łatwy sposób to zrobić. Z jumblr jest wreszcie dobre API dla Javy, które będzie działało na Androidzie. Uwierzytelnianie OAuth nie jest zabawne (nigdy nie jest), ale kiedy już to minie, jest fantastyczne.

Teraz technicznie pytanie o to, jak wykonać uwierzytelnienie, nie należy tutaj, ale jest to moje zbyt długie pytanie, więc wkleję tutaj kod i jeśli nie jest to interesujące, po prostu pomiń to.

ta wykorzystuje słoik nazwie jumblr-0.0.10-jar-z-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 wygląda następująco:

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

Dlaczego czy nie kodujesz nazwy wejściowej 'data' w parach nazwa-wartość, tak jak ty, to' type' i 'caption'? – Madbreaks

+0

Faktycznie kodowanie nazwy wejściowej nie jest konieczne (jak widać z działającego kodu dla postów tekstowych na początku posta). To tylko jedna z rzeczy, które próbowałem uruchomić. Powinieneś to powtórzyć dla jasności w przykładowym kodzie. –

+1

Pierwszą rzeczą, którą zauważam jest to, że używasz 'data', a nie' source', ale wysyłasz mu ciąg, a nie tablicę. Spróbuj użyć 'source' z zakodowanym łańcuchem lub' data' jako tablicą z jednym zakodowanym elementem ciągu, jeśli właśnie przesyłasz jedno zdjęcie. – Ally

Odpowiedz

3

Dlaczego nie używacie Jumblr oficjalny klient Java dla Tumblr.

Pozdrawiam.

+1

Z historii commitów wynika, że ​​nie istniała, gdy opublikowałem pytanie. Wygląda na to, że warto się teraz przyjrzeć. Dzięki. –

+0

Ups, może myślałem, że masz kondensator strumienia: D – eltabo

+0

Próbowałem Jumblr i nie mogłem załadować obrazu do pracy ... https://stackoverflow.com/questions/26271523/jumblr-post-photo-on- android – mythicalprogrammer

0

Ten pracował dla mnie ...

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

gdzie filePath jest http URL.

+0

Dziękuję za odpowiedź. Przesłałem poprawkę, aby uporządkować ten format - podczas zadawania pytań lub odpowiadania na pytania jest bardziej ostrożny, jeśli wcinasz kod z czterema spacjami, zamiast używać poważnych akcentów, ponieważ nie szanują twoich nowych linii. – nurdglaw

+0

Mam: {"meta": {"status": 400, "msg": "Złe żądanie"}, "odpowiedź": {"błędy": ["Błąd przesyłania zdjęcia."]}} –

0

Użyłem następującej metody. możesz spróbować tego.

// paramString = „text chcesz umieścić w podpisie”

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

EDIT: najpierw przesłać obraz do serwera WWW a następnie uzyskać adres URL i spróbuj post z przesłanego URL lub ścieżkę do pliku. będzie działać dobrze na pewno ... :)

+0

mam {" meta ": {" status ": 400," msg ":" Złe żądanie "}," odpowiedź ": {" błędy ": [" Błąd przesyłania zdjęcia. "]}} –

+0

Jestem w stanie opublikować wiadomość tekstową to nie obraz. Będzie. u pomóż mi plz –

+0

@Akanksha Spróbuj użyć edytowanego Code.above to zadziała –

0

muszę używać wieloczęściowy public class VideoUploader rozciąga 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

można łatwo zrobić za pomocą jumblr - Tumblr klienta 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

Dzięki, eltabo wspomniał o tym w innej odpowiedzi. kiedy miałem ten problem, jumblr nie istniał, teraz jest najlepszym rozwiązaniem. –

+0

edytowałem moją odpowiedź, aby odzwierciedlić to teraz. –

Powiązane problemy