Java – Android, programmatically upload photos to host on imgur

Android, programmatically upload photos to host on imgur… here is a solution to the problem.

Android, programmatically upload photos to host on imgur

I tried different ways to upload and retrieve links via imgur, but despite looking at the imgur api, none of them succeeded.

However, the following methods partially work:
I’m trying to retrieve,

Error: If any errors occur.
Link to image: Link to a hosted image
Remove link: Removes the link to the hosted image

But I only ended up getting “remove link” because everything else was blank,
Check it out:

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


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

        Iterator it = mImgurResponse.entrySet().iterator();
            HashMap.Entry pairs = (HashMap.Entry);



    } catch (IOException e) {

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

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

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

All I ended up with was a HashMap mImageResponse with only the link removed…

Any thoughts on what I did wrong?

Best Solution

The fix for this is simply changing the URL to:

Related Problems and Solutions