简体   繁体   English

将 JSON object 从网页发送到 Android 应用程序

[英]Send JSON object from webpage to Android app

I'm browsing the web to find out this, but I'm not lucky (or unskilled) in finding anything similar.我正在浏览 web 以找出这一点,但我并不幸运(或不熟练)找到类似的东西。 The project is just in the "Idea" step at the moment, and the part I'm having trouble finding a way to do is sending a JSON object from a webpage to an Android application where it would be handled.该项目目前只是在“想法”步骤中,我无法找到方法的部分是将 JSON object 从网页发送到将处理它的 Android 应用程序。 The process would be as follows:该过程如下:

  1. User fills data on webpage and submits it用户在网页上填写数据并提交
  2. Data is formatted as JSON数据格式为 JSON
  3. JSON is pushed to the Android application - need help JSON 被推送到 Android 应用程序 - 需要帮助
  4. Android application processes the JSON object Android 应用程序处理 JSON object

I presume there are specifics that need to be set on both sides (Webpage and Android) in order for it to work.我认为需要在双方(网页和 Android)上设置一些细节才能使其正常工作。 Perhaps there's an easier (correct) way to do it, maybe even different approach in general.也许有一种更简单(正确)的方法可以做到这一点,甚至可能有不同的方法。

Any help and advice is much appreciated.非常感谢任何帮助和建议。

EDIT: If it is possible I would like to avoid outside services, and would rather prefer a direct communication between the webpage and android application if it is possible.编辑:如果可能的话,我想避免外部服务,如果可能的话,我宁愿在网页和 android 应用程序之间进行直接通信。

I forgot to mention I control both the webpage and the android application我忘了提到我同时控制网页和 android 应用程序

you can use firebase cloud messaging from google firebase.google.com/docs/cloud-messaging or other service that send push message您可以使用来自谷歌firebase.google.com/docs/cloud-messaging或其他发送推送消息的服务的 firebase 云消息传递

I did something similar when building a OAuth2 Module for my company.在为我的公司构建 OAuth2 模块时,我做了类似的事情。 So my answer in principle may relate to OAuth2 but the same concept applies.所以我的回答原则上可能与 OAuth2 有关,但同样的概念也适用。

Register an activity in the AndroidManifest.xml This will be the activity that will be responsible for doing something with the return information.AndroidManifest.xml中注册一个活动,这将是负责对返回信息进行操作的活动。

Example例子

</activity>
    <activity android:name=".BaseOAuth">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="oauth"
                android:scheme="example" />
        </intent-filter>
    </activity>

In the onResume() you can use the method getIntent().getData() for which you can run querys' on.onResume()中,您可以使用getIntent().getData()方法,您可以对其运行查询。

Example例子

 @Override
    protected void onResume() {
        super.onResume();
         Uri data = getIntent().getData();
         String code = data.getQueryParameter("code") == null ? "" :data.getQueryParameter("code");
    }

In the above instance, I do provide a callback URL where the website redirects me too.在上面的例子中,我确实提供了一个回调 URL 网站也重定向了我。 example://oauth Where in the OnResume I get the information returned. example://oauth在 OnResume 中我得到返回的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM