Java 如何从startActivityForResults读取结果

Java 如何从startActivityForResults读取结果,java,android,Java,Android,团队 我的问题是,如何从startAtcivityForResult读取结果。 当按下按钮时,它将调用bStock(),从而进行URL调用并检索数据。我已经验证了URL调用是正确的,并且我确实获得了数据。 我使用了finishActivity(1)不显示实际内容或结果。为了传达这条信息,我在不使用finishActivity(1) 我的目标是读取结果,只显示某些值,如名称和最后价格。这是我的密码 public class MainActivity extends AppCompatActivi

团队

我的问题是,如何从
startAtcivityForResult
读取结果。 当按下按钮时,它将调用
bStock()
,从而进行URL调用并检索数据。我已经验证了URL调用是正确的,并且我确实获得了数据。 我使用了
finishActivity(1)
不显示实际内容或结果。为了传达这条信息,我在不使用
finishActivity(1)

我的目标是读取结果,只显示某些值,如名称和最后价格。这是我的密码

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //setContentView(R.layout.content_layout_id);

    final Button buttonStock = findViewById(R.id.buttonS);
    buttonStock.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
              bStock();
        }
    });
}

static final int REQUEST_CODE = 1;

protected void bStock() {
    String url = "http://dev.markitondemand.com/Api/v2/Quote/jsonp?symbol=AG";
    Uri uri = Uri.parse(url);
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    i.setPackage("com.android.chrome");
    startActivityForResult(i, REQUEST_CODE);
    finishActivity(1);

}

@Override
protected void onActivityResult ( int requestCode, int resultCode, Intent data) {
     if (requestCode == REQUEST_CODE) {
        // Make sure the request was successful
        //if (resultCode == RESULT_OK) { // 0 -1
        // Get the URI that points to the selected contact
        Uri o = data.getData();
        Toast.makeText(MainActivity.this, "Name ", Toast.LENGTH_LONG).show();});
  }
}
我正在使用一个
Toast
(现在)来显示名称,但我不知道如何读取数据。任何帮助都将不胜感激


Jesse

由于您尝试启动的活动(chrome浏览器)未准备好将您想要的结果返回给您,因此无法按预期工作。意图(视图)告诉浏览器,要做到这一点——查看给定的URL

对于不是您自己的活动,您必须仔细检查它们的描述,看看它们是否支持任何结果调用,以及它们如何返回结果——例如,有一个“拍摄照片”的意图,它将通过一些uri数据将拍摄的照片返回给您


大多数情况下,startActivityForResult用于启动您自己的活动,您希望将数据返回给调用活动。在这种情况下,您可以定义自己如何将结果传递回调用活动。

您可能需要遍历光标

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request it is that we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // Get the URI that points to the selected contact
            Uri contactUri = data.getData();
            // We only need the NUMBER column, because there will be only one row in the result
            String[] projection = {Phone.NUMBER};

            // Perform the query on the contact to get the NUMBER column
            // We don't need a selection or sort order (there's only one result for the given URI)
            // CAUTION: The query() method should be called from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
            // Consider using CursorLoader to perform the query.
            Cursor cursor = getContentResolver()
                    .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();

            // Retrieve the phone number from the NUMBER column
            int column = cursor.getColumnIndex(Phone.NUMBER);
            String number = cursor.getString(column);

            // Do something with the phone number...
        }
    }
}