java - 从 Parse.com 查询返回字符串

标签 java android parse-platform

所以这似乎不起作用,但话又说回来,你不能从 void 方法返回字符串。问题是我绝对需要根据我的类的结构返回一个字符串。我可以做什么来实现这个目标?我需要获取该商品的价格。

@Override
public String getCost() {
final String[] productValue = {"null"};
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Inventory");
        query.whereEqualTo("productName", "Capris");
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> list, ParseException e) {
                if (e == null) { //no exception, hence success
                    for (ParseObject productCost : list) {
                        productValue[0] = (String) productCost.get("productPrice");
                        // Cannot return a value from a method with void result type
                        return productValue[0];
                    }
                }
                else {
                    // Cannot return a value from a method with void result type
                    return null;
                }
            }
        });
        return null;
    }

最佳答案

你的条件是错误的

 @Override
    public String getCost() {
        final String[] productValue = {null};
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>
        query.whereEqualTo("productName", Capris);
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> list, ParseException e) {
                if (e == null) { //no exception, hence success
                    productValue[0] = list.get(0).getString("productPrice");
                }
            }
        });
        return productValue[0];
    }

在上面的代码中,productValue[0] 可能为 null,因为它是 aysnc 调用 因此将 findInBackground 替换为 find()

public String getCost() {
    String productValue = null;
    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Inventory");
    query.whereEqualTo("productName", "Capris");
    try {
        List<ParseObject> results = query.find();
        productValue = results.get(0).getString("productPrice");
        return productValue;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return productValue;
}

关于java - 从 Parse.com 查询返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33705660/

相关文章:

android - Eclipse Android XML 编辑器错误 :The file dose not exist

android - 实现用户选择主题

java - 将 Curl 转换为 Java

java - 获取 Parsefile 到 ImageView

java - 如何向 Eclipse Project Explorer 工具栏添加按钮?

java - 为什么在编译时不检查多重绑定(bind)泛型

java - 如何在 java 中获取已连接客户端的主机名?

java - 是否可以将对象从对象重新键入为整数

java - 如何将字符串文本添加到 orElse 和 .map

ios - PFQuery 在 Swift 3 转换后未运行