2012-10-03 36 views
1

我是新來的,我無法弄清楚這個錯誤的含義。這個錯誤的含義是什麼:方法...不適用於參數

我試過Google搜索,但不能從我發現的任何意義。

我需要與API進行交互以將票證發佈到遠程服務器,並從我正在遵循的教程中獲得此代碼。

在代碼我得到這個錯誤塊:

The method postToRemoteServer(String) in the type HelpDeskTestService is not applicable for the arguments (String, new AsyncCallback(){})

sendButton.addClickHandler(new ClickHandler() { 
     @Override 
     public void onClick(ClickEvent event) { 
      HelpDeskTestService.postToRemoteServer(
        "http://xx.xx.xx.xx/sdpapi/request/", 
        new AsyncCallback<String>() { 
         @Override 
         public void onFailure(Throwable caught) { 
          Window.alert("Failure getting XML through proxy"); 
         } 

         @Override 
         public void onSuccess(String result) { 
          processXML(result); 
         } 
        }); 
     } 
    }); 

這裏是同步接口的代碼:

public String postToRemoteServer(final String serviceUrl) 
    throws HelpDeskTestException; 

下面是從異步接口的代碼:

void postToRemoteServer(
     final String serviceUrl, 
     AsyncCallback<String> callback); 

最後,這裏是實施類的代碼:

@Override 
public String postToRemoteServer(String serviceUrl) 
     throws HelpDeskTestException { 

    try { 
     //dividing url into host: http://some.server 
     //path: a/path/in/it 
     //and parameters: this=that&those=others 

     int hostStart= serviceUrl.indexOf("//"); 

     int pathStart= serviceUrl.substring(hostStart + 2).indexOf("/"); 

     int parameterStart= serviceUrl.substring(hostStart + 2 + pathStart).indexOf("?"); 

     final String serverHost= serviceUrl.substring(0, hostStart + pathStart + 2); 

     final String serverPath= serviceUrl.substring(hostStart + 3, 
       hostStart + pathStart + 2 + parameterStart); 

     final String serverParameters= serviceUrl.substring(hostStart + pathStart + 3 + parameterStart); 

     final URL url = new URL(serverHost); 

     final URLConnection connection= url.openConnection(); 
     connection.setDoOutput(true); 

     final OutputStreamWriter out= new OutputStreamWriter(connection.getOutputStream()); 

     final BufferedReader in= new BufferedReader(new InputStreamReader(
       connection.getInputStream())); 

     out.write("POST " + serverPath + "\r\n"); 
     out.write("Host: " + serverHost + "\r\n"); 
     out.write("Accept-Encoding: identity\r\n"); 
     out.write("Connection: close\r\n"); 
     out.write("Content-Type: application/x-www-form-urlencoded\r\n"); 
     out.write("Content-Length: " + serverParameters.length() + "\r\n\r\n" + 
      serverParameters + "\r\n"); 


     String result = ""; 
     String inputLine; 

     while ((inputLine=in.readLine()) != null) { 
      result+= inputLine; 
     } 

     in.close(); 
     out.close(); 

     return result; 

    } catch (final Exception e) { 
     throw new HelpDeskTestException(); 

    } 

任何幫助將不勝感激。

+0

是'HelpDeskTestService.postToRemoteServer'靜態方法的調用? –

回答

3

調用服務時需要使用異步接口。你使用GWT.create()創建它的一個實例。假設你的異步接口被稱爲「HelpDeskTestServiceAsync」,你會做這樣的事情:

HelpDeskTestServiceAsync asyncService = GWT.create(HelpDeskTestService.class); 

asyncService.postToRemoteServer(
       "http://xx.xx.xx.xx/sdpapi/request/", 
       new AsyncCallback<String>() { 
        @Override 
        public void onFailure(Throwable caught) { 
         Window.alert("Failure getting XML through proxy"); 
        } 

        @Override 
        public void onSuccess(String result) { 
         processXML(result); 
        } 
       }); 
+0

是的!這就是它,謝謝!教程有這樣的代碼行:private final HelpDeskTestServiceAsync helpDeskTest = GWT.create(HelpDeskTest.class);我更新了上面:公共無效的onClick(ClickEvent事件){ \t \t \t \t helpDeskTest.postToRemoteServer(.... – brl8

+0

我有同樣的問題,而這個固定謝謝! – krubo

0

該錯誤表示參數表達式的類型與方法的形式參數的類型不匹配。

"The method postToRemoteServer(String) in the type HelpDeskTestService is not applicable for the arguments (String, new AsyncCallback(){})"

這是說,你想postToRemoteServer有兩個參數,但它聲明爲接受一個參數。

這並不完全清楚爲什麼會發生這種情況,但它看起來看起來就像您正在調用靜態方法一樣,並且如果是這種情況,那麼接口是沒有意義的。

另外,如果HelpDeskTestService是一個變量(嘖,嘖...代碼風格違反......嘖,嘖)那麼問題可能是變量的聲明類型。如果您聲明爲Synchronous,那麼該語言將不允許您使用Asynchronous類型的所有方法...除非Synchronous延伸Asynchronous

0

究竟是什麼說,你用一個String和一個ASyncCallback類型來調用它。它只需要一個字符串來調用。有關該方法的更多信息,請參閱該方法的API文檔或「HelpDeskTestService」類。

相關問題