【问题标题】:How to return 2 values from one method and use both values in one class?如何从一种方法返回 2 个值并在一个类中使用这两个值?
【发布时间】:2018-07-17 13:29:52
【问题描述】:

我正在试用 Microsoft 的这段代码,但我想结合他们制作的 2 个功能。一种是分析图像,一种是检测名人。但是,我很难从一个函数返回 2 个值。 这里是处理方法...

private String process() throws VisionServiceException, IOException {
    Gson gson = new Gson();
    String model = "celebrities";

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 100, output);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(output.toByteArray());


    AnalysisResult v = this.client.describe(inputStream, 1);
    AnalysisInDomainResult m = this.client.analyzeImageInDomain(inputStream,model);

    String result = gson.toJson(v);
    String result2 = gson.toJson(m);

    Log.d("result", result);

    return result, result2;
}

并用这种方法将2个结果结合起来......

    @Override
protected void onPostExecute(String data) {
    super.onPostExecute(data);

    mEditText.setText("");
    if (e != null) {
        mEditText.setText("Error: " + e.getMessage());
        this.e = null;
    } else {
        Gson gson = new Gson();
        AnalysisResult result = gson.fromJson(data, AnalysisResult.class);
        //pang detect ng peymus...
        AnalysisInDomainResult result2 = gson.fromJson(data, AnalysisInDomainResult.class);

        //decode the returned result
        JsonArray detectedCelebs = result2.result.get("celebrities").getAsJsonArray();
        if(result2.result != null){
            mEditText.append("Celebrities detected: "+detectedCelebs.size()+"\n");
            for(JsonElement celebElement: detectedCelebs) {
                JsonObject celeb = celebElement.getAsJsonObject();
                mEditText.append("Name: "+celeb.get("name").getAsString() +", score" +
                        celeb.get("confidence").getAsString() +"\n");
            }
        }else {
            for (Caption caption: result.description.captions) {
                mEditText.append("Your seeing " + caption.text + ", confidence: " + caption.confidence + "\n");
            }
            mEditText.append("\n");
        }


       /* for (String tag: result.description.tags) {
            mEditText.append("Tag: " + tag + "\n");
        }
        mEditText.append("\n");
        mEditText.append("\n--- Raw Data ---\n\n");
        mEditText.append(data);*/
        mEditText.setSelection(0);
    }
}

提前致谢!

【问题讨论】:

  • 在数组或列表(或其他集合)中,或通过复合对象
  • 使用地图。和值的不同键。 E.gL map.put(result1,val1);map.put(result2,val2);返回地图;
  • 我试过你的建议@akshayapandey。 Map.put(result, result);Map.put(result2, result2);但是,它在放置时出现错误,说“无法从静态上下文中引用非静态方法put(K,V)。如何解决这个问题?谢谢。
  • Map map = new HashMap(); map.put......

标签: java android return microsoft-cognitive


【解决方案1】:

你可以使用。参数是两个对象,所以你可以把所有东西都放上去

  final Pair<String, String> pair = Pair.create("1", "2");
  String a = pair.first;
  String b = pair.second;

【讨论】:

  • 嗨!我试过你的建议,创建这个之后我应该返回什么,Pair.create("result", result); pair.create("result2", result2);
【解决方案2】:

只需使用 Bundle

 Bundle bundle = new Bundle();
        bundle.putString("key_one", "your_first_value");
        bundle.putString("key_two", "your_second_value");
        return bundle;

您可以在 Bundle 中添加多个不同类型的值。在这种情况下,您的方法的返回类型应该是 Bundle。

【讨论】:

    【解决方案3】:

    AbstractMap#SimpleEntry(或从Java-9开始Map#entry

    您也可以随时通过Arrays.asList(one, two) 返回两个值

    【讨论】:

      猜你喜欢
      • 2010-11-30
      • 2014-06-23
      • 2019-09-28
      • 2015-05-16
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      相关资源
      最近更新 更多