2011-08-19 18 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; 
    } 

私は、バックエンドでの取得すべて削除のみリンクとハッシュマップmImageResponseです...

間違って何イム上の任意のアイデア?

+0

修正はにURL を変更するには、単にでしたapi/upload.xml – asd2005

+0

どのライブラリを使用しましたか? – emesx

+0

私はこれが年の古いことを知っていますが、私はFileBodyの例外が見つかりませんクラスを取得しています。そのクラスをどこからインポートしましたか? – PeteH

答えて

2

これまでの修正がにURLを変更するだけであった。 http://imgur.com/:これにimgur.com/api/upload.xml

+0

私は削除能力を持つ匿名アップロードを許可するアンドロイドアプリを探しています。この質問の時点で、あなたはそのようなアプリを開発していましたか?そうであれば、それをプレイストアで利用できますか?私は半ダースのアプリを試しましたが、誰も匿名でアップロードした後に削除リンクを与えませんでした! :-(ありがとう! –

関連する問題