不打印到文本框

时间:2013-08-17 13:56:47

标签: java xml-rpc

我正在尝试创建一种从哈希表中检索用户输入的articleName的authorID的方法。以下是用户按下按钮时在客户端激活的代码:

public String getAuthorID() // returns a String
    {
             try
        {
            articleName = txtArticleName.getText();
            argAuthorID = new Vector();// create vector for the args
            argAuthorID.addElement(articleName);// name to search for to get AuthorID

            // make the call to the server
            authorIDVector = (Integer)client.execute("GetSize.sendAuthorID", argAuthorID);
            System.out.println(argAuthorID);
          }
            catch (XmlRpcException exception) {
            System.err.println("JavaClient: XML-RPC Consumer Fault #" +
            Integer.toString(exception.code) + ": " +
                               exception.getCause() + "" + exception.toString());
          } catch (Exception exception) {
            System.err.println("JavaClient: XML-RPC Consumer Fault #" + exception.toString());
          }
        String StrAuthorID = Integer.toString(authorID); // Cast AuthorID to String
        return StrAuthorID;
    }

这是服务器端的方法:

public int sendAuthorID(String articleNameRequest) {
        // get info from the hashtable
        aNumber = (Integer) theHashtable.getAuthorID(articleNameRequest); // was this.
        return aNumber;
    }

这是包含哈希表的类中的代码:

public int getAuthorID(String articleName)
{
    int intfoundit;
    String foundit =  (String)hashtab.get(articleName);
    System.out.print(foundit);
    intfoundit = Integer.parseInt(foundit);
    System.out.print(foundit);
    System.out.print(intfoundit);
    return intfoundit;
}

程序可以检索AuthorID但不会将其输入到文本框中。通过测试,我发现此代码抛出了异常:

catch (XmlRpcException exception) {
            System.err.println("JavaClient: XML-RPC Consumer Fault #" +
            Integer.toString(exception.code) + ": " +
                               exception.getCause() + "" + exception.toString());

这是给出的错误:

  

'JavaClient:XML-RPC消费者错误#0:   nullorg.apache.xmlrpc.XmlRpcException:java.lang.Exception:   java.lang.NumberFormatException:对于输入字符串:“3377”'

UPDATE:删除哈希表中ID号之前的空格,它不再抛出错误,但它仍然没有输入ID号到文本框中,而只是输入一个'0'

2 个答案:

答案 0 :(得分:1)

如果字符串中有空格,则似乎失败了。我们可以在您的异常跟踪中看到parseInt无法解析" 3377"并且在执行时抛出了NumberFormatException

intfoundit = Integer.parseInt(foundit);

因此,您可以尝试trim字符串,看看它是否能解决您的问题:

intfoundit = Integer.parseInt(foundit.trim());

最好你应该在保存/将键/值放在哈希表中的地方进行修剪。

答案 1 :(得分:0)

第一个问题的答案是哈希表上的ID号之前的空格,因为空格无法转换为整数。

第二个问题的答案是以下一行试图转换错误的变量

    String StrAuthorID = Integer.toString(authorID); // Cast AuthorID to String

因为整数位于AuthorID变量

我通过改变

来纠正这个问题
        authorIDVector = (Integer)client.execute("GetSize.sendAuthorID", argAuthorID);

        authorID = (Integer)client.execute("GetSize.sendAuthorID", argAuthorID);
相关问题