2015-08-20 42 views
-1

我正在向客戶端調用API。我可以使用Java來做到這一點。我現在只需要迴應。稍後我會轉換輸出。 我的代碼如下:需要來自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(); 
     } 

我收到錯誤代碼401

my url is : http://119.235.102.65/library/index.php/API/Search/basic 
my username is : admin 
my password is : 1234 

而且我要多多搜索從數據庫中,這是娜娜的關鍵字。 keyword = nana。

+0

是的。它需要授權。 – user3472252

+0

在您的代碼示例中,您沒有將您的憑據作爲請求的一部分發送出去。這些可能需要作爲標題或請求參數發送。 –

回答

0

其實我想你的REST API使用郵差其餘客戶端的谷歌Chrome和你有錯誤:

不使用Keyword=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", 
        " 
... 
... 

Image