需要来自java的rest调用吗

需要来自java的rest调用吗,java,api,rest,restful-authentication,restful-architecture,Java,Api,Rest,Restful Authentication,Restful Architecture,我正在以客户端的身份调用API。我想用java来做这件事。我现在只需要回复。稍后我将转换输出。 我的代码如下: String data = "User=admin&Password=1234&Authorization=basic&Keyword=nana"; URL url; try { url = new URL("http://119.235.102.65/library/index.php/API/Search/basic"); HttpURLConnec

我正在以客户端的身份调用API。我想用java来做这件事。我现在只需要回复。稍后我将转换输出。 我的代码如下:

String data = "User=admin&Password=1234&Authorization=basic&Keyword=nana";
URL url;
try {
    url = new URL("http://119.235.102.65/library/index.php/API/Search/basic");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

    int responseCode = conn.getResponseCode();
        System.out.println("Response code: " + responseCode);

        boolean isSuccesResponse = responseCode < 400;

        InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream();

        wr.close();
         } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
我还要从数据库中搜索一个关键字,那就是娜娜。
keyword=nana。

事实上,我使用POSTMAN rest client for Google Chrome尝试了您的rest api,您出现了错误:

不要使用
关键字=nana

用法:
keyword=nana

答案是:

{
    "responsecode": 0,
    "responsemessage": "Success",
    "result": [
        {
            "BOOKID": "1",
            "BOOKTITLE": "Nana in the City",
            "BOOKCATALOGNUMBER": "abc 123",
            "BOOKDESCRIPTION": "In this magical picture book, a young boy spends an overnight visit with his nana and is frightened to find that the city where she lives is filled with noise and crowds and scary things. But then Nana makes him a special cape to help him be brave, and soon the everyday sights, sounds, and smells of the city are not scary—but wonderful. The succinct text is paired with watercolor illustrations that capture all the vitality, energy, and beauty of the city.",
            "PUBLISHERNAME": "Clarion Books",
            "PUBLISHERCITY": "New York",
            "TOTALCOPIES": "2",
            "AVAILABLECOPIES": "1",
            "LOANEDCOPIES": "1",
            "AUTHOR": [
                {
                    "AUTHORFNAME": "Lauren",
                    "AUTHORMNAME": null,
                    "AUTHORLNAME": "Castilo"
                }
            ],
            "BOOKCOPIES": [
                {
                    "REFERENCENUMBER": "123456",
                    "COPYNUMBER": "1",
                    "ISBN": "978-0544104433",
                    "EDITION": "1",
                    "PUBLICATIONYEAR": "2014",
                    "LOANEDSTATUS": "1"
                },
                {
                    "REFERENCENUMBER": "1234567",
                    "
...
...

是的。它需要授权。在代码示例中,您没有将凭据作为请求的一部分发送。这些可能需要作为头或请求参数发送。
{
    "responsecode": 0,
    "responsemessage": "Success",
    "result": [
        {
            "BOOKID": "1",
            "BOOKTITLE": "Nana in the City",
            "BOOKCATALOGNUMBER": "abc 123",
            "BOOKDESCRIPTION": "In this magical picture book, a young boy spends an overnight visit with his nana and is frightened to find that the city where she lives is filled with noise and crowds and scary things. But then Nana makes him a special cape to help him be brave, and soon the everyday sights, sounds, and smells of the city are not scary—but wonderful. The succinct text is paired with watercolor illustrations that capture all the vitality, energy, and beauty of the city.",
            "PUBLISHERNAME": "Clarion Books",
            "PUBLISHERCITY": "New York",
            "TOTALCOPIES": "2",
            "AVAILABLECOPIES": "1",
            "LOANEDCOPIES": "1",
            "AUTHOR": [
                {
                    "AUTHORFNAME": "Lauren",
                    "AUTHORMNAME": null,
                    "AUTHORLNAME": "Castilo"
                }
            ],
            "BOOKCOPIES": [
                {
                    "REFERENCENUMBER": "123456",
                    "COPYNUMBER": "1",
                    "ISBN": "978-0544104433",
                    "EDITION": "1",
                    "PUBLICATIONYEAR": "2014",
                    "LOANEDSTATUS": "1"
                },
                {
                    "REFERENCENUMBER": "1234567",
                    "
...
...