Android Java Bing搜索Api HTTP错误401
我没有获得对我的azure帐户的有效连接。 始终显示Http错误401-->错误身份验证 几个小时后我想不出解决办法Android Java Bing搜索Api HTTP错误401,java,android,api,search,bing,Java,Android,Api,Search,Bing,我没有获得对我的azure帐户的有效连接。 始终显示Http错误401-->错误身份验证 几个小时后我想不出解决办法 AsyncTask<Void, Void, Void> task = new AsyncTask <Void, Void, Void> () { protected Void doInBackground(Void... args) { String searchText = "Android";
AsyncTask<Void, Void, Void> task = new AsyncTask <Void, Void, Void> () {
protected Void doInBackground(Void... args) {
String searchText = "Android";
searchText = searchText.replace(" ", "%20");
String accountKey = "myazureaccountkey";
accountKey = accountKey.replace("+", "%2B");
byte[] accountAccess = ("accountKey:" + accountKey).getBytes();
byte[] accountAccessBytes = Base64.encode(accountAccess, Base64.DEFAULT);
String accountAccessString = new String(accountAccessBytes);
final String defaultURL = "https://api.datamarket.azure.com/Bing/Search/v1/Image?Query=%27";
InputStream inputStream = null;
try {
URL url = new URL(defaultURL + searchText + "%27" + "&$format=Atom");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Basic " + accountAccessString);
if (conn.getResponseCode() >= 400) {
inputStream = conn.getErrorStream();
} else {
inputStream = conn.getInputStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
task.execute();
AsyncTask task=new AsyncTask(){
受保护的Void doInBackground(Void…args){
String searchText=“Android”;
searchText=searchText.replace(“,“%20”);
字符串accountKey=“myazureaccountkey”;
accountKey=accountKey.replace(“+”,“%2B”);
字节[]accountAccess=(“accountKey:+accountKey).getBytes();
字节[]accountAccessBytes=Base64.encode(accountAccess,Base64.DEFAULT);
String accountAccessString=新字符串(accountAccessBytes);
最终字符串defaultURL=”https://api.datamarket.azure.com/Bing/Search/v1/Image?Query=%27";
InputStream InputStream=null;
试一试{
URL URL=新URL(默认URL+searchText++%27“+”&$format=Atom);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“GET”);
conn.setRequestProperty(“授权”、“基本”+accountAccessString);
如果(连接getResponseCode()>=400){
inputStream=conn.getErrorStream();
}否则{
inputStream=conn.getInputStream();
}
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
};
task.execute();
您是否查看过accountAccessString
?格式正确吗?你是否尝试过使用类似“谢谢”的工具登录?我明天将试用此工具。我查看了accountAccessString位,但我不知道它是否错误,或者应该如何。代码不是自制的。在MSAzure的web界面中,url起作用。但不知何故,它不接受auth.byte[]accountAccessBytes=Base64.encode(accountAccess,Base64.DEFAULT);他错了!您需要将apache lib与Base64函数一起使用