我想连接我的android应用程序shutterstock api,以便它可以搜索一些图像那里。它使用https scheme + Basic Authentication标头来允许用户进行所有搜索请求。我在使用HttpsURLConnection的常规Java项目中实现了功能,并能够获得正确的JSON响应。 Java代码看起来是这样的:Android应用程序连接到shutterstock api抛出IOException异常401
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();//proxy);
String username = "62c01aa824222683004b", password = "dc4ad748a75e4e69ec853ad2435a62b700e66164";
String encoded = Base64.getEncoder().encodeToString((username+":"+password).getBytes("UTF-8"));
System.out.println(encoded.equals("Nj0jMDFhZWE4ZmE4MjY4MzAwNGI6ZGM0YWQ3NDhhNzVlNGU2gWVjODUzYWQ0ZmEzYTYyYjc7MGU2NjE2NA==")); // prints true
urlConnection.setRequestProperty("Authorization", "Basic "+encoded);
当移植到Android的这一点,与401错误代码抛出一个IOException。作为上所以在很多职位描述(如一个here),我修改了代码相应的一个额外的try-catch
如下:
String username = "62c01aa824222683004b", password = "dc4ad748a75e4e69ec853ad2435a62b700e66164", encoded = "";
encoded = Base64.encodeToString((username+":"+password).getBytes("UTF-8"), Base64.URL_SAFE);
Log.e("test", "encoded strings match:" + encoded.equals("Nj0jMDFhZWE4ZmE4MjY4MzAwNGI6ZGM0YWQ3NDhhNzVlNGU2gWVjODUzYWQ0ZmEzYTYyYjc7MGU2NjE2NA==") + "\n" + encoded); // prints false but string is same!!
URL url = new URL(reqUrl);
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Basic "+encoded);
try {
if (connection != null) {
connection.connect();
if (200 == connection.getResponseCode()) { // ---> throws IOException
BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
String output;
while ((output = br.readLine()) != null) {
Log.e("test", output);
response.append(output);
}
connection.disconnect();
return response.toString();
}
}
} catch (IOException e) {
try {
Log.e("test", e.getMessage()); // ---> prints "No authentication challenges found"
Log.e("test", connection.getResponseCode() + ":" + connection.getResponseMessage() + connection.getHeaderFields());
//---> prints 401:Unauthorized{null=[HTTP/1.1 401 Unauthorized], cache-control=[no-cache], Connection=[keep-alive], Content-Length=[38], Content-Type=[application/json; charset=utf8], Date=[Tue, 31 May 2016 14:11:28 GMT], Server=[nginx], X-Android-Received-Millis=[1464703888222], X-Android-Sent-Millis=[1464703887592], x-end-user-request-id=[f754ec7f-c344-431b-b641-360aabb70184], x-shutterstock-app-version=[apitwo-625], x-shutterstock-resource=[/v2/images/search]}
if (401 == connection.getResponseCode()) {
InputStream es = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(es));
String output;
while ((output = br.readLine()) != null) {
Log.e("test", output); // ---> prints {"message":"Invalid auth credentials"}
response.append(output);
}
connection.disconnect();
return response.toString();
} else {
Log.e("test","Could not connect! " + connection.getResponseCode() + ":" + connection.getResponseMessage() + ". " + connection.getRequestMethod());
}
}catch (Exception e1){e1.printStackTrace();}
}
我无法检查响应头在Firefox的REST客户端,因为它不发当我添加Authentication标头时,请求到服务器。
所以这里的问题是:
- 这是处理在Android上的401错误的正确方法?我会在内部try-catch中获得JSON响应吗?
- java程序使用与Android中完全相同的编码字符串。为什么String.equals()在java中返回“true”,而在android中返回“false”?
- 来自服务器的错误消息显示“身份验证凭据无效”。出于任何原因,Android和Java之间的编码字符串是否有所不同?如果是的话,那么第二点是有道理的。
1)做web请求的正确方法是使用库(比较https://github.com/BitFountain/ShutterDroid/blob/master/app/src/main/java/io/bitfountain/matthewparker/shutterdroid /api/ShutterStock.java),HttpUrlConnection非常复杂:https://www.tbray.org/ongoing/When/201x/2012/01/17/HttpURLConnection 2)不是相同的字符串,equals方法保证是相同的PC和Android 3)我想是的。 – zapl
@zapl - 感谢您使用库的建议。我第一次使用HttpClient并有同样的问题。所以尝试使用HUC。我想平等法在PC和Android上完美地完成了它的工作。但是,Android中“假”的原因没有意义。我将编码后的字符串从PC复制到字符串中,瞧!有效 ;) – Ashok