2011-08-19 16 views
5

İmgur api'ye rağmen bir bağlantı yüklemek ve almak için farklı yöntemler denedim ama hiçbiri başarılı olmadı. Herhangi bir hata oluştu eğer: http://api.imgur.com/examples#uploading_javaAndroid, imgur programında barındırmak için bir fotoğraf yüklüyor

Ama aşağıdaki yöntemler kısmen im,

hataları geri almaya .. çalışır. Resme linki:

: Görüntü

barındırılan Ama sadece başkalarının boş olarak, bunu kontrol, "bağlantısını sil" ile bitirmek silme bağlantısını: görüntüye bağlantı silme bağlantısı barındırılan
public void post(String path) { 


    List<NameValuePair> postContent = new ArrayList<NameValuePair>(2); 
    postContent.add(new BasicNameValuePair("key", DEV_KEY)); 
    postContent.add(new BasicNameValuePair("image", path)); 


    String url = "http://api.imgur.com/2/upload"; 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpContext localContext = new BasicHttpContext(); 
    HttpPost httpPost = new HttpPost(url); 

    try { 
     MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 

     for(int index=0; index < postContent.size(); index++) { 
      if(postContent.get(index).getName().equalsIgnoreCase("image")) { 
       // If the key equals to "image", we use FileBody to transfer the data 
       entity.addPart(postContent.get(index).getName(), new FileBody(new File (postContent.get(index).getValue()))); 
      } else { 
       // Normal string data 
       entity.addPart(postContent.get(index).getName(), new StringBody(postContent.get(index).getValue())); 
      } 
     } 

     httpPost.setEntity(entity); 

     HttpResponse response = httpClient.execute(httpPost, localContext); 
     mImgurResponse = parseResponse (response); 


     Iterator it = mImgurResponse.entrySet().iterator(); 
     while(it.hasNext()){ 
      HashMap.Entry pairs = (HashMap.Entry)it.next(); 

      Log.i("INFO",pairs.getKey().toString()); 
      if(pairs.getValue()!=null){ 
       reviewEdit.setText(pairs.getValue().toString()); 

      Log.i("INFO",pairs.getValue().toString()); 
      } 
     } 


    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private Map<String,String> parseResponse(HttpResponse response) { 
     String xmlResponse = null; 

     try { 
      xmlResponse = EntityUtils.toString(response.getEntity()); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     if (xmlResponse == null) return null; 

     HashMap<String, String> ret = new HashMap<String, String>(); 
     ret.put("error", getXMLElementValue(xmlResponse, "error_msg")); 
     ret.put("delete", getXMLElementValue(xmlResponse, "delete_page")); 
     ret.put("original", getXMLElementValue(xmlResponse, "original_image")); 

     return ret; 
    } 

private String getXMLElementValue(String xml, String elementName) { 
     if (xml.indexOf(elementName) >= 0) 
      return xml.substring(xml.indexOf(elementName) + elementName.length() + 1, 
        xml.lastIndexOf(elementName) - 2); 
     else 
      return null; 
    } 

geri sonunda elde Tüm ne im sadece silme bağlantı ile bir HashMap mImageResponse ...

herhangi bir fikir yanlış yapıyor?

+0

: http://imgur.com/ api/upload.xml – asd2005

+0

Hangi kütüphaneleri kullandınız? – emesx

+0

Bu yılın eski olduğunu biliyorum ama FileBody için bir sınıf istisnası bulamıyorum. Bu sınıfı nereden ithal ettiniz? – PeteH

cevap

2

Buna düzeltme URL'yi değiştirmek için sadece oldu: imgur.com/api/upload.xml buna düzeltme URL'yi değiştirmek için sadece oldu

+0

Silmek yeteneği ile anonim yüklemeye izin veren bir android uygulaması arıyorum. Herhangi bir şansı bu tür bir uygulama geliştirirken bu soru, ve eğer evet, oyun deposunda kullanılabilir mi? Yarım düzine uygulamayı denedim, ancak hiçbiri anonimously yükledikten sonra silme bağlantısı vermedi! :-(Teşekkürler! –

İlgili konular