【问题标题】:Use HashMap in XMLRPC result在 XMLRPC 结果中使用 HashMap
【发布时间】:2023-03-15 04:34:01
【问题描述】:

我是 Android 开发新手,我正在尝试使用 XMLRPC 在 RESULT 中接收 HashMap,但每次应用程序崩溃时,这是我的代码,请给我建议:

  Object RESULT =  XMLRPCClient.callEx(methodname,new Object[] {params});
         Map FRESULT= (Map) RESULT; 

【问题讨论】:

  • 您收到的错误信息是什么?
  • "应用程序......已意外停止请重试"
  • 需要从Logcat获取实际的异常信息。如果问题不是很明显(我猜是 ClassCastException),那么有人可以进一步帮助您。

标签: java android hashmap xml-rpc


【解决方案1】:

我也一直在处理这个问题,并设法以这种方式获取值:

try {
    Object[] answer = (Object[]) client.call("call", sessionId, method, params);
    HashMap map = (HashMap) answer[0]; // get first item of the response because in my case the response was an array of Objects with one item in it holding the HashMap
    Object[] records = (Object[]) map.get("records"); // I only needed values from "records" key
    for (int i = 0; i < records.length; i++) {
        HashMap record = (HashMap) records[i]; // create another map from the records values, in my case uid's of categories
        Category cat = new Category(); // creating new instance of my Category class
        cat.setCatUid((String) record.get("uid")); // calling a method of the Category class to set Uid to the value from record HashMap
        m_categories.add(cat); // this adds it to my ArrayList<Category>
    }
} catch (XMLRPCException e) {
    Log.e(method, "Exception", e);
}

我确定这是一团糟,我自己是 Java 新手,但它对我有用。希望对你有帮助:)

【讨论】:

    【解决方案2】:

    现在应用程序在实施后和平地通过了:

     Object RESULT = XmlRpcConnect.ServerCall_a(method,new Object[] {params});
              Map<String, Object> FRESULT= (HashMap<String, Object>) RESULT;
    

    在我的 XmlRpcConnect 类中进行了一些更改:

    @SuppressWarnings("unchecked");
    public static Object ServerCall_a(String method, Object[] params){
                XMLRPCClient client = new XMLRPCClient(server);
                HashMap<String, Object> result=null;
                      try{
                result = (HashMap<String, Object>) client.callEx(method, params);
                                                    }
                      catch(XMLRPCFault f){
                          //   result = ("Fault message: " + f.getMessage());
                                                                    }
                      catch(XMLRPCException e){
                          // result = ("Exception message: " + e.getMessage());
                                                                    }
                           return result;
                                    }
    

    但是当尝试提取值时它再次崩溃,任何建议:

                  if (FRESULT.get("status") == null) {
                              result = (String) FRESULT.get("status");
                              toastDialog(result);
                   }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 2010-11-23
      • 2010-09-09
      • 1970-01-01
      相关资源
      最近更新 更多