从Java Android运行curl [英] Run curl from Java android
本文介绍了从Java Android运行curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个curl命令
curl -X GET "https://api.spotify.com/v1/search?q=Carlos+Vives&type=artist" -H "Accept: application/json" -H "Authorization: Bearer <My API Key>"
如何运行它,并从Android(Java)获取JSON响应?
How can I run it ,and get the JSON response from Android (Java)?
推荐答案
我找到了解决方案,谢谢
I've found the solution , Thanks
/**
* Gets the response from http Url request
* @param url
* @return
* @throws IOException
*/
public static String getResponseFromHttpUrl(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.addRequestProperty("Accept","application/json");
connection.addRequestProperty("Content-Type","application/json");
connection.addRequestProperty("Authorization","Bearer <spotify api key>");
try {
InputStream in = connection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A");
boolean hasInput = scanner.hasNext();
if (hasInput) {
return scanner.next();
} else {
return null;
}
} finally {
connection.disconnect();
}
}
这篇关于从Java Android运行curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文