Java 无法让HttpClient执行Imgur API v3 post请求
我正在尝试构建一个匿名上传到imgurapiv3的应用程序,这似乎会导致Java 无法让HttpClient执行Imgur API v3 post请求,java,android,api,upload,imgur,Java,Android,Api,Upload,Imgur,我正在尝试构建一个匿名上传到imgurapiv3的应用程序,这似乎会导致NullPointerException。我的清单中添加了互联网权限,所以我不确定为什么这不起作用。由于明显的原因,客户端id被删除。如有任何建议,将不胜感激 public HttpResponse uploadImage(String image_contents) { /* defining imgur api location */ String API = ("https://api.imgur.co
NullPointerException
。我的清单中添加了互联网权限,所以我不确定为什么这不起作用。由于明显的原因,客户端id
被删除。如有任何建议,将不胜感激
public HttpResponse uploadImage(String image_contents) {
/* defining imgur api location */
String API = ("https://api.imgur.com/3/image.json");
Uri imgurAPI = (Uri.parse(API));
// url encoding for bitmap
// String dev_key = ("anon-dev-key-removed");
String client_id = ("client-id-removed");
/* String client_secret = ("secret-id-removed"); */
/* constructing query */
// spawning apache httpclient and post instance
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(API);
// defining namevaluepairs for post data and setting entity
// httpclient help from this url:
// http://www.vogella.com/articles/ApacheHttpClient/article.html
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("image", image_contents));
/* for debugging purposes */
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
/* end for debugging purposes */
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// adding oauth2 header with clientid
httppost.addHeader("Authorization", "Client-ID " + client_id);
// execute the post and collect the response as a httpresponse object
HttpResponse response = null;
changeText(httppost.toString());
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* shut down http client */
httpclient.getConnectionManager().shutdown();
return response;
}
最近可能已经改变了。。。但是使用ApacheHTTP客户端和IMGUR3.0API(至少在我为其编写代码时是这样)。您必须为HTTP客户端添加一些特殊的SSL处理代码。如果您不这样做,您的请求将失败:
// io exception with the message: (hostname in certificate didn't match: <api.imgur.com> != <imgur.com> OR <imgur.com>)
//io异常,消息为:(证书中的主机名不匹配:
在发出请求时(在catch块中),您是否看到任何异常?如果不是,可能SSL配置不再是一个问题。u将需要发出HTTPS请求而不是HTTP bez您的Web服务url包含HTTPS协议而不是HTTP@ρ∑ρρK。我可能是错的,但我认为只要您提供正确的url,HttpClient就可以在本机上使用HTTPS。我只是通过切换到t来实现这一点他使用了IMGURAPIv2,但我不确定为什么它在版本3中不起作用。
// io exception with the message: (hostname in certificate didn't match: <api.imgur.com> != <imgur.com> OR <imgur.com>)