Android,以编程方式上传照片到 imgur 上托管
我尝试了不同的方法来通过 imgur 上传和检索链接,但尽管查看了 imgur api,但没有一个成功。 http://api.imgur.com/examples#uploading_java
但以下方法部分有效.. 我正在尝试检索
错误:如果发生任何错误。 图像链接:指向托管图像的链接 删除链接:删除托管图像的链接
但我最终只得到“删除链接”,因为其他都是空白的, 检查一下:
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...
关于我做错了什么有什么想法吗?
I have tried different methods to upload and retrieve a link via imgur but none have been successfull despite looking at the imgur api.
http://api.imgur.com/examples#uploading_java
But the following methods partly works..
im trying to retrieve,
errors: if any errors occured.
link to image: the link to the image hosted
delete link: the link to delete the image hosted
But i only end up with the "delete link", as the others are 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 = "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;
}
All i get back in the end is a hashmap mImageResponse with only the delete link...
any ideas on what im doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决此问题只需将 URL 更改为:imgur.com/api/upload.xml
The fix to this was merely to change the URL to: imgur.com/api/upload.xml