Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javaandroid运行curl_Java_Android_Curl - Fatal编程技术网

从javaandroid运行curl

从javaandroid运行curl,java,android,curl,Java,Android,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>" curl-X GET”https://api.spotify.com/v1/search?q=Carlos+Vives&type=artist“-H”Accept:app

我有这个卷曲命令

curl -X GET "https://api.spotify.com/v1/search?q=Carlos+Vives&type=artist" -H "Accept: application/json" -H "Authorization: Bearer <My API Key>"
curl-X GET”https://api.spotify.com/v1/search?q=Carlos+Vives&type=artist“-H”Accept:application/json“-H”Authorization:Bearer”

如何运行它并从Android(Java)获得JSON响应?

您可以使用以下方法执行命令行,也可以使用
Gson
将其更改为
JsonObject

public static String executeCommand(String command) {
    StringBuilder output = new StringBuilder();
    try {
      Process proc = Runtime.getRuntime().exec(new String[] { "sh", "-c", command });
      BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

      String line;
      while ((line = reader.readLine())!= null) {
        output.append(line + "\n");
      }
      proc.waitFor();
    } catch (Exception e) {
      e.printStackTrace();
    }

    return output.toString();
  }
[编辑]

请不要介意这个答案


正如他(cricket_007)的评论,您需要使用Android或Java的网络库,如或。

我找到了解决方案,谢谢

 /**
 * 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();
    }
}
/**
*从http Url请求获取响应
*@param-url
*@返回
*@抛出异常
*/
公共静态字符串getResponseFromHttpUrl(URL URL URL)引发IOException{
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod(“GET”);
addRequestProperty(“接受”、“应用程序/json”);
addRequestProperty(“内容类型”、“应用程序/json”);
连接。addRequestProperty(“授权”、“持有人”);
试一试{
InputStream in=connection.getInputStream();
扫描仪=新扫描仪(英寸);
scanner.useDelimiter(\\A”);
布尔hasInput=scanner.hasNext();
如果(输入){
返回scanner.next();
}否则{
返回null;
}
}最后{
连接断开();
}
}

你不会的。如果curl仍然需要,您应该将其转换为适当的JavaHTTP库方法Asynctask@cricket_007你的想法和我一样。我现在正在尝试使用它,它将被流所需要,然后我也将编辑我的答案。@cricket_007我为我的错误答案感到抱歉,非常感谢你的反馈。我相信这是可以做到的,但如果可以避免的话,我真的不应该这么做