有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    你确定你在发帖吗?上面的URL看起来像GET

    <html><head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>405 HTTP method GET is not supported by this URL</title>
    </head>
    <body text=#000000 bgcolor=#ffffff>
    <h1>Error: HTTP method GET is not supported by this URL</h1>
    </body></html>
    
  2. # 2 楼答案

    根据API documentation,应该将JSON数据作为POST方法的请求体传递,而不是作为URL参数传递

    所以它应该是这样的:

    String data = "{\"data\": [{\"text\": \"I love Titanic.\"}, {\"text\": \"I hate Titanic.\"}]}";
    
    URL url = new URL("http://www.sentiment140.com/api/bulkClassifyJson?appid=me@gmail.com");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    
    // write the request body
    connection.getOutputStream().write(data.getBytes("UTF8"));
    
    // get the response and read it
    InputStream in = connection.getInputStream();