2011-08-19 64 views
5

我已經嘗試了不同的方法來上傳和通過imgur檢索鏈接,但沒有一個已經成功,儘管看imgur api。 http://api.imgur.com/examples#uploading_javaAndroid,上傳一張照片到imgur上的主機編程式

但下面的方法部分工程.. 我嘗試找回,

錯誤:如果有錯誤發生。 鏈接圖像:鏈接到圖像託管 刪除鏈接:

:刪除的圖像託管

但我只用「刪除鏈接」,因爲其他都是空白, 看看最終的鏈接

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

我得到的背後到底是一個HashMap mImageResponse只刪除鏈接...

任何想法是什麼即時做錯了什麼?

+0

的修復這僅僅是對URL 更改爲: http://imgur.com/ api/upload.xml – asd2005

+0

你使用了什麼庫? – emesx

+0

我知道這是一年之久,但我得到一個沒有發現FileBody異常的類。你是從哪裏進口的? – PeteH

回答

2

的修復程序,這是僅僅是爲了將URL更改爲:imgur.com/api/upload.xml

+0

我正在尋找一個允許匿名上傳和刪除功能的android應用程序。在這個問題發生的時候,你有沒有機會開發這樣的應用程序?如果是,是否可以在遊戲商店購買?我試過半打應用程序,但沒有提供一個刪除鏈接上傳anonimously! :-( 謝謝! –